我已搜查,但似乎無法找到我所要完成更改單個細胞的基於細胞的DataGrid中值的顏色
的解決方案,我有一個dataGridView2和我想的細胞如果單個單元格中的值不爲空,則Superfrom列將變爲紅色
我已搜查,但似乎無法找到我所要完成更改單個細胞的基於細胞的DataGrid中值的顏色
的解決方案,我有一個dataGridView2和我想的細胞如果單個單元格中的值不爲空,則Superfrom列將變爲紅色
我假設您使用的是WinForms,並且您的列名爲Superfrom。
foreach (DataGridViewRow row in dataGridView2.Rows)
{
DataGridViewCell cell = row.Cells[Superfrom.Index];
if (cell.Value != null)
cell.Style.BackColor = Color.Red;
else
cell.Style.BackColor = Color.White;
}
您對空白的定義可能會有所不同。在這種情況下,用任何陳述將爲您的空虛情況測試替換cell.Value != null
。
foreach (DataGridViewRow row in dataGridView2.Rows)
{
DataGridViewCell cell = row.Cells[Superfrom.Index];
if (cell.Value.ToString() != String.Empty)
{
cell.Style.BackColor = Color.Red;
}
else
{
cell.Style.BackColor = Color.White;
}
}
您是否嘗試過CellPainting事件 – PLED 2013-05-04 19:48:25