0
votes

I have two comboboxes. I'm populating two comboboxes like this. Combobox 1 and 2 names are cmbpartyName and cmbprefPT2.

    SqlDataReader reader = new VotingOP().getPartyNamesToCombo();
        while (reader.Read())
        {
          cmbPartyName.Items.Add(new MyComboItem() { MyItemID = Convert.ToInt32(reader["partyID"]), MyItemName = reader["partyName"].ToString() });
        }
        reader.Close();
        cmbPartyName.ValueMember = "MyItemID";
        cmbPartyName.DisplayMember = "MyItemName";

        SqlDataReader reader01 = new VotingOP().getPartyNamesToCombo();
        while (reader01.Read())
        {

            cmbPrefPT2.Items.Add(new MyComboItem() { MyItemID = Convert.ToInt32(reader01["partyID"]), MyItemName = reader01["partyName"].ToString() });
        }
        reader01.Close();
        cmbPrefPT2.ValueMember = "MyItemID";
        cmbPrefPT2.DisplayMember = "MyItemName";

And here I'm selecting a value from combobox1 in its selected index changed event like this.

      private void cmbPartyName_SelectedIndexChanged(object sender, EventArgs e)
    {
        MyComboItem selectedItem = (MyComboItem)cmbPartyName.SelectedItem as MyComboItem;
        if (selectedItem != null)
        {

            MessageBox.Show(String.Format("You've just selected the '{0}' partyID with the Name {1}",selectedItem.MyItemID,selectedItem.MyItemName));

        }
    }

As two combo boxes are having same values how can I remove the selected value of the first combobox which is cmbparty from the second combo box cmbPrefPT2?

1

1 Answers

0
votes

You can make your MyComboItem implement IEquatable

  public class MyComboItem : IEquatable<MyComboItem>
  {
    public int MyItemId { get; set; }
    public string MyItemName { get; set; }
    public bool Equals(MyComboItem other)
    {
        return MyItemId == other.MyItemId;
    }
  }

Then you can just call the remove method of the second combobox in the event handler passing the selecteditem of the first combobox

MyComboItem selectedItem = (MyComboItem)cmbPartyName.SelectedItem as MyComboItem;
    if (selectedItem != null)
    {
         cmbPrefPT2.Items.Remove(cmbPartyName.SelectedItem);
    }