【问题标题】:Search the particular text in datagrid using C#使用 C# 在数据网格中搜索特定文本
【发布时间】:2012-11-20 09:43:07
【问题描述】:

使用 datagrid 我连接到 sqlite。我能够将 sqlite 的特定表打开到数据网格中。但是现在我想从表中搜索特定的东西,我正在使用 select 语句并应用一个文本框。用户从那里输入输入和搜索。下面是我的代码

    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字符串语句中我需要如何通过文本框输入呢?

【问题讨论】:

    标签: c# winforms datagrid


    【解决方案1】:

    就这样做

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

    【讨论】:

    • 我接受了它并放弃了投票,因为您的回答很快就让我满意
    【解决方案2】:

    您可以使用上面的 yogi 的答案,也可以直接在 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) { }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2013-05-10
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      • 2016-09-27
      • 2013-12-17
      相关资源
      最近更新 更多