检查 datagridview 单元格是否为 null 或为空 [英] Check if datagridview cell is null or empty

查看:46
本文介绍了检查 datagridview 单元格是否为 null 或为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须更改单元格的背景颜色,当它们的值为字符串或空时,这是我在这里编写的类似于其他代码的代码:

I have to change the background color of the cells , when their value is string or empty, this is the code i have write similar to other codes here :

for (int rowIndex = 0; rowIndex < dataGridView1.RowCount; rowIndex++)
            {
             string conte = dataGridView1.Rows[rowIndex].Cells[7].Value.ToString()  ;
                if (string.IsNullOrEmpty(conte))
                {
                // dataGridView1.Rows[rowIndex].Cells[7].Style.BackColor = Color.Orange;
                }
                else
                 { dataGridView1.Rows[rowIndex].Cells[7].Style.BackColor = Color.Orange; }
        } 

数据集已完成,向我展示填充的 datagridview 并向我展示此错误:

the dataset is complete , show me the datagridview populated and the show me this error:

我该如何解决这个问题??有其他方法来编写代码吗?

How can i fix this?? Have another way to write the code?

推荐答案

我会使用以下类似的方法遍历单元格..

I would iterate through the cells using something like the following..

foreach (DataGridViewRow dgRow in dataGridView1.Rows)
{
    var cell = dgRow.Cells[7];
    if (cell.Value != null)   //Check for null reference
    {
        cell.Style.BackColor = string.IsNullOrEmpty(cell.Value.ToString()) ? 
            Color.LightCyan :   
            Color.Orange;       
    }
}

这篇关于检查 datagridview 单元格是否为 null 或为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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