简体   繁体   English

C#使用一个文本框过滤具有多行的搜索数据表

[英]C# Filter search Datatable with multiple rows using one textbox

I have created a filter search for my attendance monitoring system , it searches by the employee name but not with other fields, I think I'm gonna use the LIKE keyword but I don't know how to do it. 我为考勤监控系统创建了一个过滤器搜索,它按员工姓名搜索,但不按其他字段搜索,我想我会使用LIKE关键字,但我不知道该怎么做。 Here's my code 这是我的代码

private void textBox1_TextChanged(object sender, EventArgs e)
{ 
    connection.Open();
    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    string query = "SELECT * FROM tblEmployee WHERE [Firstname] like @1";
    command.parameters.AddWithValue("@1",textBox1.Text);
    command.CommandText = query;
    OleDbDataAdapter da = new OleDbDataAdapter(command);
    DataTable dt = new DataTable();
    da.Fill(dt);
    dataGridView1.DataSource = dt;
    dataGridView1.Update();
    dataGridView1.Refresh();
    connection.Close(); 
}

The behaviour matches exactly with the query you wrote, you should rewrite the query to match your requirements: 该行为与您编写的查询完全匹配,您应该重写查询以符合您的要求:

string query = "SELECT * FROM tblEmployee WHERE [Firstname] like '%' + @1 + '%' OR [OtherColumn] LIKE '%' + @1 + '%'";

BTW, I have changed slightly your LIKE to make it behave like a 'it does contain XXX'. 顺便说一句,我已经稍微更改了您的“ LIKE ,使其表现为“包含XXX”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM