I have a checkbox in a column of a DataGridView. Right now I got an exception because there is no checkbox checked. I am not sure how to handle it?
private void uncheckGrid(ref DataGridView dgv, int index)
{
try
{
foreach (DataGridViewRow row in dgv.Rows)
{
DataGridViewCheckBoxCell check = row.Cells[1] as DataGridViewCheckBoxCell;
if (check.Value != null) // throw an exception here.
{
if ((bool)check.Value)
{
Int32 intVal = Convert.ToInt32(row.Cells[0].Value);
if (intVal == index)
{
check.Value = false;
}
}
}
}
Thanks.
DataGridViewCheckBoxCell check = row.Cells[1] as DataGridViewCheckBoxCell;is an implicit cast. It may be returning null because the row cell is not truly aDataGridViewCheckBoxCell.Add an additional condition to the if statement after the cast: