类似于所示和提到的:
注意:考虑到单元格将在 DataGridView 控件可见后更改其颜色(仅) 。因此,一种实用的解决方案是使用:
VisibleChanged 事件
如果您希望在创建新行时保持您的风格;还订阅:
行添加事件
示例如下:
///<summary> Instantiate the DataGridView Control. </summary>
private DataGridView dgView = new DataGridView;
///<summary> Method to configure DataGridView Control. </summary>
private void DataGridView_Configuration()
{
// In this case the method just contains the VisibleChanged event subscription.
dgView.VisibleChanged += DgView_VisibleChanged;
// Uncomment line bellow in case you want to keep the style when creating new rows.
// dgView.RowsAdded += DgView_RowsAdded;
}
///<summary> The actual Method that will re-design (Paint) DataGridView Cells. </summary>
private void DataGridView_PaintCells()
{
int nrRows = dgView.Rows.Count;
int nrColumns = dgView.Columns.Count;
Color green = Color.LimeGreen;
// Iterate over the total number of Rows
for (int row = 0; row < nrRows; row++)
{
// Iterate over the total number of Columns
for (int col = 0; col < nrColumns; col++)
{
// Paint cell location (column, row)
dgView[col, row].Style.BackColor = green;
}
}
}
///<summary> The DataGridView VisibleChanged Event. </summary>
private void DataGridView_VisibleChanged(object sender, EventArgs e)
{
DataGridView_PaintCells();
}
/// <summary> Occurrs when a new Row is Created. </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
DataGridView_PaintCells();
}
最后:只需调用 DataGridView_Configuration() (方法),
即:表单加载事件。