【问题标题】:How to remove item selected from combobox from another combobox in c#如何从C#中的另一个组合框中删除从组合框中选择的项目
【发布时间】:2019-04-05 15:18:50
【问题描述】:

来自两个组合框的项目列表是相同的。我的问题是,如果我从第一个组合框中选择一个项目,我想从第二个组合框中删除或不显示该项目,但该项目将保留在第一个组合框中。

//////////////this is how i fill combobox
public void fillcombobox()
    {
        String selectQuery = "SELECT * FROM candidate_list";
        connection.Open();
        cmd = new MySqlCommand(selectQuery, connection);
        mdr = cmd.ExecuteReader();
        try
        {
            while (mdr.Read())
            {
               if (mdr.GetString("candidate_position").Equals("PRO"))
                {
                    //PRO1
                    cbo_Pro1.Text = "-- SELECT CANDIDATE --";
                    cbo_Pro2.Text = "-- SELECT CANDIDATE --";
                    string fname = mdr.GetString("candidate_name");
                    string spacee = " ";
                    string lname = mdr.GetString("candidate_surname");
                    cbo_Pro1.Items.Add(fname + spacee + lname);
                    cbo_Pro2.Items.Add(fname + spacee + lname);
                }
            }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        connection.Close();
    }


////////////this is the event for 1st combobox

private void cbo_Pro1_SelectedIndexChanged(object sender, EventArgs e)
    {
        pro1Name = cbo_Pro1.Text.Split(null);

        String selectQuery = "Select * FROM candidate_list WHERE candidate_name LIKE '%" + pro1Name[0] + "%'";

        cmd = new MySqlCommand(selectQuery, connection);

        da = new MySqlDataAdapter(cmd);

        DataTable table = new DataTable();


        da.Fill(table);

        byte[] image = (byte[])table.Rows[0][5];

        pictureBox8.Image = byteArrayToImage(image);

        da.Dispose();

    }

//////////////////this is the event for second combobox

private void cbo_Pro2_SelectedIndexChanged(object sender, EventArgs e)
    {
        pro2Name = cbo_Pro2.Text.Split(null);

        String selectQuery = "Select * FROM candidate_list WHERE candidate_name LIKE '%" + pro2Name[0] + "%'";

        cmd = new MySqlCommand(selectQuery, connection);

        da = new MySqlDataAdapter(cmd);

        DataTable table = new DataTable();


        da.Fill(table);

        byte[] image = (byte[])table.Rows[0][5];

        pictureBox9.Image = byteArrayToImage(image);

        da.Dispose();

    }

【问题讨论】:

  • 你应该做的是创建一个可以过滤的主视图,不需要每次都加载数据......然后你过滤视图......

标签: c# winforms combobox


【解决方案1】:

你说两个 ComboBox 的项目完全相同,

这很简单:

private void cbo_Pro1_SelectedIndexChanged(object sender, EventArgs e)
{
     cbo_Pro2.Items.Remove(cbo_Pro1.Text);
}

基本上,当您在 cbo_Pro1 中选择一个项目时,删除 cbo_Pro2 上的该特定项目..

【讨论】:

  • 哦,哇,我想太多了,我想解决我的问题。解决方案非常简单。哎呀XD。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2013-12-04
  • 1970-01-01
  • 1970-01-01
  • 2014-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多