C# 在winform datagrid C中更改单元格背景颜色#

C# 在winform datagrid C中更改单元格背景颜色#,c#,winforms,datagrid,C#,Winforms,Datagrid,我想根据c#windows应用程序中的值更改背景数据网格单元格。例如,如果单元格值为3,则单元格背景色设置为蓝色,如果单元格值等于2,则单元格背景色更改为红色。您可以使用单元格格式事件: private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex == 1) { if ((int)

我想根据c#windows应用程序中的值更改背景数据网格单元格。例如,如果单元格值为3,则单元格背景色设置为蓝色,如果单元格值等于2,则单元格背景色更改为红色。

您可以使用单元格格式事件:

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;
        }
}

您可以使用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;
}

我希望这有帮助。

你想要这样的东西吗

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;
}
我希望这有帮助