I am using winforms and C#,.

When i try to get datagridviewcheckbox state, if the checkbox not true it shows “Null Reference Exception was caught” as Exception.
My Code is
foreach (DataGridViewRow fees_row in this.dataGridView2.Rows)
{
if ((bool) fees_row.Cells[0].Value == true)
{
}
}
The error is in the line:
if ((bool) fees_row.Cells[0].Value == true)
how to set datagridview checkbox value not null. or escape this exception.
You need to make sure that the
DataGridViewCellobject is notnullfirst, before trying to interrogate its value. That’s what’s causing theNullReferenceException—you’re interrogating theValueproperty of anullobject!Change your code to look like this:
But before someone else leaves a snarky comment, there’s generally no reason to check a Boolean value against a literal
true. All you have to write isif (boolValue)