我有多行的Datagridview,如果任何行中的任何单元格值为空,那么我想将单元格颜色更改为红色 [英] I Have Datagridview In That Multiple Rows ,In That If Any Cell Value In Any Row Is Null Then I Want To Change Cell Color To Red

查看:108
本文介绍了我有多行的Datagridview,如果任何行中的任何单元格值为空,那么我想将单元格颜色更改为红色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多行的datagridview,如果任何行中的任何单元格值为null,那么我想将单元格颜色更改为红色,那么如何将该特定空单元格的颜色更改为红色...?

解决方案

将代码添加到datagridview_cellpainting事件中。如下所示



  private   void  dataGridView2_CellPainting( object  sender,DataGridViewCellPaintingEventArgs e)
{

foreach (DataGridViewRow row in dataGridView2.Rows)
{
for int i = 0 ; i < dataGridView2.Columns.Count - 1 ; i ++)
{
if (row.Cells [i] .Value.ToString()== null || row.Cells [i] .Value.ToString()== // 您设置背景颜色红色的条件
{


行。 DefaultCellStyle.BackColor = Color.Red;

}
}

}


}


i have datagridview in that multiple rows ,in that if any cell value in any row is null then i want to change cell color to red, so how to change that particular null cell's color to red...?

解决方案

Add code to the datagridview_cellpainting event.As follows


private void dataGridView2_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{

    foreach (DataGridViewRow row in dataGridView2.Rows)
    {
        for(int i=0; i < dataGridView2.Columns.Count - 1; i++)
        {
        if (row.Cells[i].Value.ToString() == null || row.Cells[i].Value.ToString()=="") //your condition to set backcolor red
        {


            row.DefaultCellStyle.BackColor = Color.Red;

        }
        }

    }


}


这篇关于我有多行的Datagridview,如果任何行中的任何单元格值为空,那么我想将单元格颜色更改为红色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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