Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在未设置为对象实例的C#-对象引用中迭代DataGridView_C#_Winforms_Datagridview_Nullreferenceexception - Fatal编程技术网

在未设置为对象实例的C#-对象引用中迭代DataGridView

在未设置为对象实例的C#-对象引用中迭代DataGridView,c#,winforms,datagridview,nullreferenceexception,C#,Winforms,Datagridview,Nullreferenceexception,我有以下代码: foreach (DataGridViewRow r in DataGridObject.Rows) { MyDataSource.ForEach( delegate( Product p) { if (r.Cells["Product"].Value.ToString() == p.Title)

我有以下代码:

        foreach (DataGridViewRow r in DataGridObject.Rows)
        {
            MyDataSource.ForEach( 
                delegate( Product p)
                {
                    if (r.Cells["Product"].Value.ToString() == p.Title)
                    {
                        tally += p.Price;
                    }
                }
            );
        }
在运行时的if语句中,我得到了
错误

An unhandled exception of type 'System.NullReferenceException' 
occurred in WindowsFormsApplication1.exe

Additional information: Object reference not set to an instance of an object.
有什么问题吗

你知道我哪里出错了吗?

像这样做吧

             foreach (DataGridViewRow r in DataGridObject.Rows)
    {
        MyDataSource.ForEach( 
            delegate( Product p)
            {
                if (!string.isNullorWhiteSpace(Convert.ToString(r.Cells["Product"].Value)) && Convert.ToString(r.Cells["Product"].Value).Equals(p.Title,StringComparison.OrdinalIgnoreCase))
                {
                    tally += p.Price;
                }
            }
        );
    }
你有没有写着“产品”的专栏?你能试试索引吗

r.Cells[1].Value
像这样的

另外,您可能需要将其转换为特定的单元格类型?如果它是一个textboxcell,那么你可能不得不这么做

r.Cells[1].Text

需要检查cell.Value for null

r.Cells[“Product”]
为null,并且您正在尝试访问
Value
r.Cells[“Product”]。Value
为null,并且您正在尝试对其调用
ToString()
也可能是
p
为null,但可能性较小-您尝试过调试吗?嗯。r、 Cells[“Product”]没有达到我当时认为应该达到的效果。如何从列中获取值?r.Cells[“Product”]。值可以为null几乎所有
NullReferenceException
的情况都是相同的。请参阅“”,以获得一些提示。我试图编辑男生的答案,但他不接受,因此我将用你的答案作为答案。@JamesT:基本上,
版主审查了你的编辑并拒绝了。不是我!!但是我建议你让
null
检查
r.Cells[“Product”]
r.Cells[“Product”]。Value
他们到底为什么要这样做。我很抱歉,布拉!我个人认为你的答案要全面得多。也许我现在会受到惩罚,但我会将你的答案标记为正确答案。@huMptyduMpty它将返回null,if语句将失败。为什么不起作用?
 foreach (DataGridViewRow r in DataGridObject.Rows)
        {
            MyDataSource.ForEach( 
                delegate( Product p)
                {
                    if(r.Cells["Product"]!=null)
                     {
                      if(r.Cells["Product"].Value!=null)
                      {
                        if (r.Cells["Product"].Value.ToString() == p.Title)
                        {
                          tally += p.Price;
                        }
                      }
                    }
                }
            );
        }