c# - 当我测试检查它们时,为什么未更改的 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.'
解决方案
我找到了解决方案!
我在datagridview加载行后初始化了复选框
foreach (DataGridViewRow r in DGV.Rows)
{
var cbxCell = (DataGridViewCheckBoxCell)r.Cells[0];
cbxCell.Value = "false";
}
推荐阅读
- apache-spark - object has no attribute 'map' error in pyspark 2.4.4
- java - How to Delete files from the directory by specifying it in the output application
- python-3.x - sys.path 中的 /usr/lib/python37.zip
- c++ - 最大子数组和和那些索引-分而治之
- google-cloud-storage - 如何将文件从 Google Cloud Storage 导入在 R 中运行的 H2O
- asp.net-core - ASP.NET Core WebAPI 中的 POST 服务
- google-chrome - 从移动设备打开已安装的 Web 应用程序时出现问题(添加到主屏幕)
- c++ - 在 Windows 中通过 C/C++ 从 BITMAP[+WAVE(s)] 到 RTSP (Twitch) 的路径是什么?
- javascript - 如何在 HTML 中设置条件?
- javascript - 如何从 redux 的嵌套结构中创建/编辑/删除?