private void textBox1_TextChanged(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = "ColumnName like '%" + textBox1.Text + "%'";
dataGridView1.DataSource = bs;
}
我不想指定列。如何在所有列中搜索数据?
答案 0 :(得分:1)
对于示例,请尝试从文本框中连续找到四个值:
private void textBox1_TextChanged(object sender, EventArgs e)
{
string Query = "";
if (textBox1.Text != string.Empty)
Query += "ColumnName like '%" + textBox1.Text.Trim() + "%' and ";
if (textBox2.Text != string.Empty)
Query += "ColumnName2 like '%" + textBox2.Text.Trim() + "%' and ";
if (textBox3.Text != string.Empty)
Query += "ColumnName3 like '%" + textBox3.Text.Trim() + "%' and ";
if (textBox4.Text != string.Empty)
Query += "ColumnName4 Like '%" + textBox4.Text.Trim() + "%' and ";
Query += "1 = 1";
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = Query;
}
或在所有列中找到它:
private void textBox1_TextChanged(object sender, EventArgs e)
{
string Query = "";
if (textBox1.Text != string.Empty)
Query += "ColumnName like '%" + textBox1.Text.Trim() + "%' or ";
if (textBox2.Text != string.Empty)
Query += "ColumnName2 like '%" + textBox1.Text.Trim() + "%' or ";
if (textBox3.Text != string.Empty)
Query += "ColumnName3 like '%" + textBox1.Text.Trim() + "%' or ";
if (textBox4.Text != string.Empty)
Query += "ColumnName4 Like '%" + textBox1.Text.Trim() + "%' or ";
Query += "1 = 1";
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = Query;
}