2013-12-11 15 views
0

我想添加一行篩選功能在我的DataGridView但不斷收到以下錯誤消息:行篩選器崩潰,System.Data.EvaluateException C#應用程序是未處理的消息

System.Data.EvaluateException was unhandled 
Message: An unhandled exception of type 'System.Data.EvaluateException' occurred in System.Data.dll 
Additional information: Cannot perform 'Like' operation on System.Int32 and System.String. 

這種情況發生,每次我在我的文本框中輸入內容。任何想法爲什麼?代碼如下:

的App.config

​​

Form1.cs的

// driver no search 

    private void driverNo_TextChanged(object sender, EventArgs e) 
    { 
     BindingSource bs = new BindingSource(); 
     bs.DataSource = dataGridView1.DataSource; 
     bs.Filter = "DriverNo like '%" + driverNo.Text + "%'"; 
     dataGridView1.DataSource = bs; 
    } 
+0

邊評論:我不沒有想到製作一個無限深的巢穴層次ed BindingSources是一個好主意。 –

+0

謝謝 - 你會建議什麼?這是我在論壇上遇到的最簡單的方法 – theshizy

+0

最簡單的方法是始終使用一個BindingSource,然後相應地設置Filter屬性,而不是創建新的和嵌套。 –

回答

0

試試這個在Form1.cs

private void driverNo_TextChanged(object sender, EventArgs e) 
    { 

     if (string.IsNullOrEmpty(driverNo.Text)) 
     { 
      ((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = string.Empty; 
      return; 
     } 

     int _driverNo; 

     if (int.TryParse(driverNo.Text,out _driverNo)) 
      ((DataTable)DataGridViews.DataSource).DefaultView.RowFilter = "DriverNo = " + _driverNo; 
     else 
      MessageBox.Show("Invalid driver no."); 
    } 
+0

代碼沒有改變? – theshizy

+0

@ Antoine-LaurentLavoisier Change是''DriverNo like%「+ driverNo.Text +」%「;'用這個替換你的舊代碼並重新運行代碼。我從過濾字符串中刪除」'「。 –

+0

謝謝 - 我現在得到以下錯誤消息'在System.Data.dll中發生類型'System.Data.SyntaxErrorException'的未處理的異常' – theshizy

相關問題