2012-11-20 32 views
0

使用datagrid我已連接到sqlite。我可以打開sqlite的特定表格到數據網格中。但現在我想從表中搜索特定的東西,我使用select語句並應用文本框。用戶從那裏輸入並輸入數據。以下是我的代碼使用C#在datagrid中搜索特定的文本#

private void button2_Click(object sender, EventArgs e) 
    { 
     SQLiteConnection connection2 = new SQLiteConnection(@"Data Source = C:\ssds\WEBATM\APTRABuilder.sqlite;Version =3"); 
     connection2.Open(); 
     string sql2 = "Select *from builderScreenResourceBundleTBL where screenId like '%_textboxSearch%'"; 
     SQLiteDataAdapter connect2 = new SQLiteDataAdapter(sql2, connection2); 
     DataSet ds2 = new DataSet(); 
     connect2.Fill(ds2); 
     dataGridView.DataSource = ds2.Tables[0]; 
    } 

所以在sql2字符串語句中,我需要如何通過文本框輸入?

回答

2

只是這樣做

string sql2 = 
"Select * from builderScreenResourceBundleTBL where screenId like '%"+YourTextBox.Text+"%'"; 
+0

非常感謝,它的工作 – Sohail

+0

我接受了它並且給了你投票權,因爲你的回答在更快的時間裏讓我滿意 – Sohail

0

您可以使用瑜伽士的答案上面,或者你可以直接適用於你的DataGridView的過濾器,這將是這個樣子:

private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
     try 
     { 
      ((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = string.Format("screenId like '%{0}%'", textBox1.Text.Trim().Replace("'", "''")); 
     catch (Exception) { } 

    }