检查datagridview单元格是null还是空

ale*_*ero 5 c# datagridview visual-studio

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

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; }
        } 
Run Code Online (Sandbox Code Playgroud)

数据集完成后,显示填充的datagridview并显示此错误: 在此输入图像描述

我怎样才能解决这个问题??有另一种方法来编写代码吗?

Mas*_*ick 7

我将使用以下内容迭代细胞.

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;       
    }
}
Run Code Online (Sandbox Code Playgroud)