【问题标题】:DataGridViewButtonCell button color change on click单击时 DataGridViewButtonCell 按钮颜色更改
【发布时间】:2016-09-08 23:02:03
【问题描述】:

我有一个DataGridViewRow,我以编程方式将其添加到DataGridView“表”中。我希望其中一个单元格是DataGridViewButtonCell,以便我可以单击它。 (我意识到我可以在 on_click 的 datagridview 上有一个处理程序,但为了代码的简单性,我想保持这种方式)。

我希望能够设置 DataGridViewButtonCell 的前景色/背景色。

DataGridViewRow dgvRow = new DataGridViewRow();
dgvRow = (DataGridViewRow)dgv.Rows[0].Clone();
dgvRow.Cells[0].Value = filename;
dgvRow.Cells[0].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

DataGridViewButtonCell dgvBtn = new DataGridViewButtonCell();
dgvRow.Cells[1].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dgvBtn.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

dgvBtn.Style.BackColor = Color.Green; //this sets the color of the cell, not the button in the cell.

问:如何设置单元格中出现的按钮颜色?

【问题讨论】:

    标签: c# button datagridview background-color


    【解决方案1】:

    与普通的Button 非常相似,您需要将其设为FlatStyle Button 以更改BackColor

    dgvBtn.FlatStyle = FlatStyle.Flat; 
    dgvBtn.Style.BackColor = Color.LightGreen;
    

    【讨论】: