如何在 Windows Forms C# 中根据字段更改 DataTable 行的颜色(例如:如果这些字段为空,我想更改行的颜色)?
Datatable 有什么属性吗?
您需要像 MSDN 上显示的那样连接到 datagridview 的 CellFormatting 事件,如下所示:
private void dataGridView1_CellFormatting(object sender,
System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
// check against your column name here
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Balance"))
{
// we are now in the correct column
String stringValue = e.Value as string;
DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
switch (stringValue)
{
case "high":
cell.Style.BackColor = Color.Red;
break;
case "medium":
...
break;
}
}
}