0

两个问题:

  1. 你将如何合并这两段代码?将它们分成两个大块似乎有点多余!

  2. 您将如何通过多列进行搜索(如果在以下列中的任何一列中找到结果,则返回结果:姓名、性别、年龄)?

    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;
    }
    
4

1 回答 1

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 + "%'";
于 2013-11-02T20:44:50.137 回答