两个问题:
您如何合并两位代码?将它们作为两个大块似乎有点多余!
您将如何通过多个列进行搜索(如果在以下任何列中找到结果,则返回结果:名称,性别,年龄)?
private void button1_Click(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = "id like '%" + textBox1.Text + "%'";
dataGridView1.DataSource = bs;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = "id like '%" + textBox1.Text + "%'";
dataGridView1.DataSource = bs;
}
答案 0 :(得分:1)
回答您的第一个问题:
创建一个函数来获取您的搜索语句,并在需要时调用它。如下所示:
private void button1_Click(object sender, EventArgs e)
{
SearchData();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
SearchData();
}
private void SearchData()
{
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = "id like '%" + textBox1.Text + "%'";
dataGridView1.DataSource = bs;
}
回答第二个问题:
您可以按如下方式编写过滤器:
bs.Filter = "id like '%" + textBox1.Text + "%' and name like '%" + textBox2.Text + "%'";