C# 使用可空类型:boolean

C# 使用可空类型:boolean,c#,C#,我在DataGridView的列中有一个复选框。现在我遇到了一个异常,因为没有选中复选框。我不知道怎么处理 private void uncheckGrid(ref DataGridView dgv, int index) { try { foreach (DataGridViewRow row in dgv.Rows) { DataGridViewCheckBoxCell

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

谢谢。

DataGridViewCheckBoxCell check=row.Cells[1]作为DataGridViewCheckBoxCell是隐式强制转换。它可能返回null,因为行单元格不是真正的
DataGridViewCheckBoxCell

在强制转换后向if语句添加附加条件:

if (check != null && check.Value != null)
{ /* Do stuff here */ }

DataGridViewCheckBoxCell check=row.Cells[1]作为DataGridViewCheckBoxCell是隐式强制转换。它可能返回null,因为行单元格不是真正的
DataGridViewCheckBoxCell

在强制转换后向if语句添加附加条件:

if (check != null && check.Value != null)
{ /* Do stuff here */ }

也许你用“as”强制转换无效?什么样的异常,哪一行?也许你用“as”强制转换无效?什么样的异常,哪一行?