2016-05-12 245 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;