繁体   English   中英

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

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

我想基于C#Windows应用程序中的值更改背景datagird单元格。 例如,如果单元格值为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;
}

我希望这有帮助。

暂无
暂无

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

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