Check if datagridview cell is null or empty

This question already has an answer here:

  • What is a NullReferenceException, and how do I fix it? 33 answers

  • 我会遍历使用类似下面的东西的单元格..

    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;       
        }
    }
    

    You don't need ToString() . See here.

    The below code is enough.

    string conte = dataGridView1.Rows[rowIndex].Cells[7].Value;
    

    Also you may try something like:

    if (string.IsNullOrEmpty(dataGridView1.Rows[0].Cells[7].Value as string))
    
    链接地址: http://www.djcxy.com/p/28048.html

    上一篇: 对象引用未设置为ASP.NET中对象的实例

    下一篇: 检查datagridview单元格是否为空或空