簡體   English   中英

C#Combobox SelectedIndexChanged沒有更新datagridview

[英]C# Combobox SelectedIndexChanged is not updating the datagridview

我有2個帶有團隊名稱列表的組合框(它們被標記為comboBox2和comboBox3)。 我正在使用SelectedIndexChanged事件,以便在選擇團隊時,兩個datagrid視圖將基於團隊名稱顯示搜索到的條件。 以下是兩個組合框的代碼:

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。 如果您需要任何進一步的解釋,請告訴我。

第二個按鈕不應該更新第二個datagridview嗎?

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

在我看來,您正在更新同一個DGV的兩倍...?

檢查以確保DropDownStyle屬性設置為與comboBox2相同的屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM