2016-05-12 246 views
0

我有一个DataGridViewRow,我编程添加到DataGridView“表”。我希望其中一个单元格是DataGridViewButtonCell,所以我可以点击它。 (我知道我可以在的on_click datagridview上有一个处理程序,但为了简化代码,我想保持这种方式)。点击DataGridViewButtonCell按钮颜色变化

我希望能够设置DataGridViewButtonCell的forecolor/backcolor。

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. 

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

回答

1

很像一个普通Button你需要使它成为一个FlatStyleButton改变BackColor

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