【发布时间】:2012-10-02 08:10:17
【问题描述】:
我想根据 c# windows 应用程序中的值更改背景数据网格单元格。例如,如果单元格值为 3 单元格背景颜色设置为蓝色如果单元格值等于 2 单元格背景颜色更改为红色。
【问题讨论】:
我想根据 c# windows 应用程序中的值更改背景数据网格单元格。例如,如果单元格值为 3 单元格背景颜色设置为蓝色如果单元格值等于 2 单元格背景颜色更改为红色。
【问题讨论】:
您可以使用 CellFormatting 事件:
private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 1)
{
if ((int)e.Value == 3)
e.CellStyle.BackColor = Color.Blue;
if ((int)e.Value == 2)
e.CellStyle.BackColor = Color.Red;
}
}
【讨论】:
你想要这样的东西
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[someColumnIndex].Value == 3)
row.Cells[someColumnIndex].Style.BackColor = System.Drawing.Color.Blue;
else if (row.Cells[someColumnIndex].Value == 2)
row.Cells[someColumnIndex].Style.BackColor = System.Drawing.Color.Red;
}
我希望这会有所帮助。
【讨论】: