0

我在 DataGridView 的列中有一个复选框。现在我遇到了一个异常,因为没有选中复选框。我不知道如何处理它?

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

谢谢。

4

1 回答 1

3

DataGridViewCheckBoxCell check = row.Cells[1] as DataGridViewCheckBoxCell;是一个隐式转换。它可能返回 null 因为行单元格不是真正的 a DataGridViewCheckBoxCell

在强制转换后的 if 语句中添加一个附加条件:

if (check != null && check.Value != null)
{ /* Do stuff here */ }
于 2012-09-19T18:09:32.343 回答