C# C我在制作文本框以在DGV中的列中查找名称时遇到了NullReferenceException

C# C我在制作文本框以在DGV中的列中查找名称时遇到了NullReferenceException,c#,winforms,datagridview,C#,Winforms,Datagridview,我对文本框查找有问题,我有一个程序,用方法开始查找,将三个字母放入文本框,它会找到所有与三个起始字母匹配的。这是我使用的代码 private void findTextBox_TextChanged(object sender, EventArgs e) { var bd = (BindingSource)debtDGV.DataSource; var dt = (DataTable)bd.DataSource; dt.Defaul

我对文本框查找有问题,我有一个程序,用方法开始查找,将三个字母放入文本框,它会找到所有与三个起始字母匹配的。这是我使用的代码

    private void findTextBox_TextChanged(object sender, EventArgs e)
    {
        var bd = (BindingSource)debtDGV.DataSource;
        var dt = (DataTable)bd.DataSource;
        dt.DefaultView.RowFilter = string.Format(" '{0}%'", findTextBox.Text.Trim().Replace("'", "''"));
        debtDGV.Refresh();
    }

您可以使用此代码逐个检查行,并检查textbox值是否包含单元格的值

                foreach (DataGridViewCell oneCell in dataGridView1.SelectedCells)
                {
                    string data = (string)dataGridView1[0, a].Value; //0 is column index here
                    if (data.StartsWith(textBox1.Text)) // You can use Contains instead of StartsWith to search all the value
                    {
                        //What happens when You find the specified cell
                    }
                }
如果需要,可以使用另一个循环检查所有单元格。
此代码仅适用于第一列的单元格

什么是空值?你附上调试器了吗?在方法的第一行放置断点并调试它。在调试器中逐行执行,查看哪一项为null。