简体   繁体   English

更改Winform DataGrid C#中的单元格背景颜色

[英]change cell background color in winform datagrid C#

I want change background datagird cell base on value in c# windows application. 我想基于C#Windows应用程序中的值更改背景datagird单元格。 for instance if cell value is 3 cell background color set to blue if cell value equal 2 cell background color change to red . 例如,如果单元格值为3,则单元格背景色设置为蓝色;如果单元格值等于2,则单元格背景色更改为红色。

You can use CellFormatting event: 您可以使用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;
        }
}

You want some thing like this 你想要这样的事情

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

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM