如何有条件地更改数据网格视图单元格的颜色 [英] how to change color of a datagrid view cell conditionally

查看:120
本文介绍了如何有条件地更改数据网格视图单元格的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,



我有一个datagridview,我用sql server数据库中的表填充它。现在我想设置一个条件,如果例如datagridview的cell [2,3]的值为5,它的颜色必须是红色;



我该怎么做?



谢谢。

hi friends,

i have a datagridview that i fill it with a table in sql server database. now i want to set a condition that if for example cell[2,3] of datagridview has value of 5 it color must be red;

how can i do this?

thank you.

推荐答案

您可以在下面的CellFormatting事件中执行此操作。

You can do this on CellFormatting event like below.
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    // give the column name which you want to check the value 
    if (this.dataGridView1.Columns[e.ColumnIndex].Name == "ColumnName")
    {
        if (e.Value != null)
        {
            string stringValue = (string)e.Value;
            if (stringValue == "5")
            {
                e.CellStyle.BackColor = Color.Red;
            }

        }
    }
}


尝试 dataGridView1 .Rows [2] .Cells [3] .Style.BackColor = Color.Red


这篇关于如何有条件地更改数据网格视图单元格的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆