C#Combobox SelectedIndexChanged未更新datagridview

时间:2017-10-05 14:49:25

标签: c# datagridview

我有2个组合框,其中包含一个团队名称列表(标记为comboBox2& comboBox3)。我正在使用SelectedIndexChanged事件,以便在选择团队时,2个数据网格视图将根据团队名称显示搜索的条件。以下是两个组合框的代码:

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindingSource bs = new BindingSource();
            BindingSource bs2 = new BindingSource();
            bs.DataSource = dataGridView1.DataSource;
            bs2.DataSource = dataGridView2.DataSource;

            string filter = "";
            string filter2 = "";

            // Check if text fields are not null before adding to filter. 
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                filter += dataGridView1.Columns["Name"].HeaderText.ToString() + " LIKE '%" + textBox1.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(comboBox1.Text))
            {
                if (filter.Length > 0) filter += "AND ";
                filter += dataGridView1.Columns["Position"].HeaderText.ToString() + " LIKE '%" + comboBox1.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(comboBox2.Text))
            {
                if (filter.Length > 0) filter += "AND ";
                filter += dataGridView1.Columns["Team"].HeaderText.ToString() + " LIKE '%" + comboBox2.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(comboBox3.Text))
            {
                if (filter.Length > 0) filter += "AND ";
                filter += dataGridView1.Columns["Team"].HeaderText.ToString() + " LIKE '%" + comboBox3.Text + "%' ";
            }

            bs2.Filter = filter2;
            dataGridView1.DataSource = bs2;
            Injuries();


            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                filter2 += dataGridView2.Columns["Name"].HeaderText.ToString() + " LIKE '%" + textBox1.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(comboBox1.Text))
            {
                if (filter2.Length > 0) filter2 += "AND ";
                filter2 += dataGridView2.Columns["Position"].HeaderText.ToString() + " LIKE '%" + comboBox1.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(comboBox2.Text))
            {
                if (filter2.Length > 0) filter2 += "AND ";
                filter2 += dataGridView2.Columns["Team"].HeaderText.ToString() + " LIKE '%" + comboBox2.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(comboBox3.Text))
            {
                if (filter2.Length > 0) filter2 += "AND ";
                filter2 += dataGridView2.Columns["Team"].HeaderText.ToString() + " LIKE '%" + comboBox3.Text + "%' ";
            }

            bs2.Filter = filter2;
            dataGridView2.DataSource = bs2;
            Injuries();

        }

-

 private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindingSource bs = new BindingSource();
        BindingSource bs2 = new BindingSource();
        bs.DataSource = dataGridView1.DataSource;
        bs2.DataSource = dataGridView2.DataSource;

        string filter = "";
        string filter2 = "";

        // Check if text fields are not null before adding to filter. 
        if (!string.IsNullOrEmpty(textBox1.Text))
        {
            filter += dataGridView1.Columns["Name"].HeaderText.ToString() + " LIKE '%" + textBox1.Text + "%' ";
        }
        if (!string.IsNullOrEmpty(comboBox1.Text))
        {
            if (filter.Length > 0) filter += "AND ";
            filter += dataGridView1.Columns["Position"].HeaderText.ToString() + " LIKE '%" + comboBox1.Text + "%' ";
        }
        if (!string.IsNullOrEmpty(comboBox2.Text))
        {
            if (filter.Length > 0) filter += "AND ";
            filter += dataGridView1.Columns["Team"].HeaderText.ToString() + " LIKE '%" + comboBox2.Text + "%'";
        }
        if (!string.IsNullOrEmpty(comboBox3.Text))
        {
            if (filter.Length > 0) filter += "AND ";
            filter += dataGridView1.Columns["Team"].HeaderText.ToString() + " LIKE '%" + comboBox3.Text + "%'";
        }

        bs2.Filter = filter2;
        dataGridView1.DataSource = bs2;
        Injuries();


        if (!string.IsNullOrEmpty(textBox1.Text))
        {
            filter2 += dataGridView2.Columns["Name"].HeaderText.ToString() + " LIKE '%" + textBox1.Text + "%' ";
        }
        if (!string.IsNullOrEmpty(comboBox1.Text))
        {
            if (filter2.Length > 0) filter2 += "AND ";
            filter2 += dataGridView2.Columns["Position"].HeaderText.ToString() + " LIKE '%" + comboBox1.Text + "%' ";
        }
        if (!string.IsNullOrEmpty(comboBox2.Text))
        {
            if (filter.Length > 0) filter += "AND ";
            filter += dataGridView1.Columns["Team"].HeaderText.ToString() + " LIKE '%" + comboBox2.Text + "%'";
        }
        if (!string.IsNullOrEmpty(comboBox3.Text))
        {
            if (filter.Length > 0) filter += "AND ";
            filter += dataGridView1.Columns["Team"].HeaderText.ToString() + " LIKE '%" + comboBox3.Text + "%'";
        }
    }

我遇到的问题是

if (!string.IsNullOrEmpty(comboBox2.Text))
            {
                if (filter.Length > 0) filter += "AND ";
                filter += dataGridView1.Columns["Team"].HeaderText.ToString() + " LIKE '%" + comboBox2.Text + "%' ";
            }

工作正常,但

 if (!string.IsNullOrEmpty(comboBox3.Text))
                {
                    if (filter.Length > 0) filter += "AND ";
                    filter += dataGridView1.Columns["Team"].HeaderText.ToString() + " LIKE '%" + comboBox3.Text + "%' ";
                }

根本不会更新datagridview。如果您需要任何进一步的解释,请告诉我。

2 个答案:

答案 0 :(得分:1)

不应该有第二个按钮更新第二个datagridview吗?

 if (!string.IsNullOrEmpty(comboBox3.Text))
                {
                    if (filter.Length > 0) filter += "AND ";
                    filter += dataGridView2.Columns["Team"].HeaderText.ToString() + " LIKE '%" + comboBox3.Text + "%' ";
                }

在我看来,你正在更新两次相同的DGV ......?

答案 1 :(得分:0)

检查以确保将DropDownStyle属性设置为与comboBox2上的属性相同