Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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中搜索不工作-System.NullReferenceException错误_C#_Datagridview - Fatal编程技术网

C# 在datagridview中搜索不工作-System.NullReferenceException错误

C# 在datagridview中搜索不工作-System.NullReferenceException错误,c#,datagridview,C#,Datagridview,我试图在datagridview上的第2列(数据类型:number)中运行搜索,但不断收到以下错误消息: An unhandled exception of type 'System.NullReferenceException' occurred in SpeedyRent.exe Additional information: Object reference not set to an instance of an object. 如果(!string.Equals(row.Cells[

我试图在datagridview上的第2列(数据类型:number)中运行搜索,但不断收到以下错误消息:

An unhandled exception of type 'System.NullReferenceException' occurred in SpeedyRent.exe
Additional information: Object reference not set to an instance of an object.

如果(!string.Equals(row.Cells[1].Value.ToString(),driverNo.Text,StringComparison.OrdinalIgnoreCase))

我做错了什么?我已将我的代码包括在下面:

    void driverSearch()
    {
        CurrencyManager manager = (CurrencyManager)BindingContext[dataGridView1.DataSource];
        manager.SuspendBinding();
        bool shouldNotFilter = string.IsNullOrEmpty(driverNo.Text);
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (shouldNotFilter)
            {
                row.Visible = true;
            }
            else
            {
                if (!string.Equals(row.Cells[1].Value.ToString(), driverNo.Text, StringComparison.OrdinalIgnoreCase))
                {
                    row.Visible = false;
                }
                else
                {
                    row.Visible = true;
                }
            }
        }
        manager.ResumeBinding();
    }

    private void driverNo_KeyUp(object sender, KeyEventArgs e)
    {
        driverSearch();
    }

    private void driverNo_TextChanged(object sender, EventArgs e)
    {
        driverSearch();
    }

    private void driverNo_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
        {
            e.Handled = true;
        }

        driverSearch();
    }

在foreach循环中尝试此块:

if (shouldNotFilter)
{
     row.Visible = true;
}
else
{
    if(row.Cells[1].Value == null)
    {
       row.Visible = false;
    }
    else
    {
         if (!string.Equals(row.Cells[1].Value.ToString(), driverNo.Text, StringComparison.OrdinalIgnoreCase))
         {
              row.Visible = false;
         }
         else
         {
              row.Visible = true;
         }
    }
}

是否有少于3个单元格或空值的行
row.Cells[2].Value.ToString()
可能是问题的根源,如果少于3个单元格(即2个单元格不存在),或者如果单元格的值为null。是的,你确定你不是指
Cells[1]
?我刚刚尝试了Cells[1]-得到了相同的错误消息,你可以告诉我们错误发生在哪一行。或者甚至调试该行并找出空值。如果(!string.Equals(row.Cells[1].Value.ToString(),driverNo.Text,StringComparison.OrdinalIgnoreCase)),则会在
处抛出错误。