首页 > 解决方案 > 当我测试检查它们时,为什么未更改的 DatagridViewCheckboxCells 会抛出空异常?

问题描述

我的代码抛出空异常,甚至不会捕获块。

foreach (DataGridViewRow r in DGV.Rows)
{
  var cbxCell = (DataGridViewCheckBoxCell)r.Cells[0];
  if (cbxCell.Value.ToString() == "true") **<<----- here the exception gets thrown**
  {
     montantFAC = Convert.ToDouble(r.Cells[4].Value.ToString());
     cmd = new SqlCommand(....);

当我在已检查的情况下进行测试时,它会遍历已检查的行,甚至是我之前检查过的未检查行和之后未检查过的行。但是一旦它进入未更改的检查单元格,就会引发异常。如果您需要更多信息,请告诉我。谢谢你!

这是一个例外:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

标签: c#.net

解决方案


我找到了解决方案!

我在datagridview加载行后初始化了复选框

foreach (DataGridViewRow r in DGV.Rows)
                    {
                        var cbxCell = (DataGridViewCheckBoxCell)r.Cells[0];
                        cbxCell.Value = "false";
                    }

推荐阅读