2017-05-25 34 views
-4

我有問題的文本框查找我有這段程序使「Starts With」方法找到它通過將三個字母放入文本框它會找到所有匹配的三個首字母。下面是我用C#我得到NullReferenceException當我在DGV列中找到名稱的文本框

代碼
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(); 
    } 
+0

什麼是null?你有附加調試器嗎?在方法的第一行放置一個斷點並對其進行調試。在調試器中一行一行地去查看哪些東西是空的。 – Stuart

回答

0

您可以使用此代碼通過一個檢查行之一,並檢查文本框的值含有細胞的值

   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 
        } 
       } 

您可以使用另一個循環檢查到所有細胞如果你想。 此代碼僅適用於第一列的單元格

相關問題