P. Pat
P. Pat

Reputation: 508

Using dataGridView1.Rows[i].Cells[0].Value.ToString() != "" in datagridview_CellFormatting shows value is null and throws null error

I have this two codes below:

Method:

public void GetGridColor()
{
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
        if (dataGridView1.Rows[i].Cells[0].Value.ToString() != "")
        {
            if (dataGridView1.Rows[i].Cells[10].Value.ToString() == "Normal")
            {
                dataGridView1.Rows[i].Cells[10].Style.BackColor = Color.FromArgb(157, 196, 230);
            }

            if (dataGridView1.Rows[i].Cells[10].Value.ToString() == "Critical")
            {
                dataGridView1.Rows[i].Cells[10].Style.BackColor = Color.FromArgb(47, 117, 181);
            }

            if (dataGridView1.Rows[i].Cells[10].Value.ToString() == "High")
            {
                dataGridView1.Rows[i].Cells[10].Style.BackColor = Color.FromArgb(31, 78, 120);
            }
        }
    }
}

CellFormatting Event:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[0].Value.ToString() != "")
                {
                    if (dataGridView1.Rows[i].Cells[10].Value.ToString() == "Normal")
                    {
                        dataGridView1.Rows[i].Cells[10].Style.BackColor = Color.FromArgb(157, 196, 230);
                    }

                    if (dataGridView1.Rows[i].Cells[10].Value.ToString() == "Critical")
                    {
                        dataGridView1.Rows[i].Cells[10].Style.BackColor = Color.FromArgb(47, 117, 181);
                    }

                    if (dataGridView1.Rows[i].Cells[10].Value.ToString() == "High")
                    {
                        dataGridView1.Rows[i].Cells[10].Style.BackColor = Color.FromArgb(31, 78, 120);
                    }
                }
            }
        }

Which basically should do the exact same thing. But it throws a System.NullReferenceException.

Now if I change it to

if (dataGridView1.Rows[i].Cells[0].Value != null)

in CellFormatting event it fixes the problem. I have a bit of knowledge in programming which I admit is lacking but my question is without changing any FormattedValue or providing custom conversion from the cell value to the display value, why is it throwing a null exception error when Cells[0] does not contain null value on my sample data? And when I applied the same code using ToString() on a different datagridview(lets call it datagridview2) with a different but almost exact sample data, no error is shown. Also reading this to try to understand why error is showing DataGridView CellFormating.

Upvotes: 0

Views: 7329

Answers (1)

null and "" are different from each other. I would suggest you to check both in the condition section.

if (String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[0].Value as String))

Upvotes: 0

Related Questions