Multiple Combo Boxes With The Same Data Source (C#)

probably you could also try this solution, just assign a new Context to the 2nd combo box:

                combobox1.DataSource = results;
                combobox1.DisplayMember = "DisplayValue";
                combobox1.ValueMember = "Value";

                combobox2.BindingContext = new BindingContext();   //create a new context
                combobox2.DataSource = results;
                combobox2.DisplayMember = "DisplayValue";
                combobox2.ValueMember = "Value";

Thank you


Yes , absolutely you are correct since you are binding to the same source , so selecting in one will be applied to the rest of the combox

for overcoming to this problem , you need to manually remove the other combobox handler in a slectedindex changed event then set selected index and then again add handlers to put in code , just see below

    ComboBox c1 = new ComboBox();
        ComboBox c2 = new ComboBox();

        c1.SelectedIndexChanged += new EventHandler(c1_SelectedIndexChanged);


        c2.SelectedIndexChanged += new EventHandler(c2_SelectedIndexChanged);

    void c2_SelectedIndexChanged(object sender, EventArgs e)
    {
        c1.SelectedIndexChanged -= c1_SelectedIndexChanged;
        c2.SelectedIndex = 2;
        c1.SelectedIndexChanged += c1_SelectedIndexChanged;

    }

    void c1_SelectedIndexChanged(object sender, EventArgs e)
    {
        c2.SelectedIndexChanged -= c2_SelectedIndexChanged;
        c1.SelectedIndex = 2;
        c2.SelectedIndexChanged += c2_SelectedIndexChanged;
    }

I don't see why this should be so hard... you can just link them to clones of the same data source... that solves the problem. All you need to do is

objInsuredPrice.DataSource = new List<TSPrice>(priceList);
objTPPrice.DataSource = new List<TSPrice>(priceList);
objProvSum.DataSource = new List<TSPrice>(priceList);

Incidentally, this is exactly what VVS' code does.

Still, weird behaviour... that just has to be a bug, imo.


I know you didn't ask for it but may I suggest you to refactor your final code a little bit :-)

private List<TSPrice> GetPriceList()
{
  return new List<TSPrice>
             {
               new TSPrice(0, ""),
               new TSPrice(0, "Half Day"),
               new TSPrice(0, "Full Day"),
               new TSPrice(0, "1 + Half"),
               new TSPrice(0, "2 Days"),
               new TSPrice(0, "Formal Quote Required")
             };
}

private void BindPriceList(ComboBox comboBox, List<TSPrice> priceList)
{
  comboBox.DataSource = priceList();
  comboBox.ValueMember = "Price";
  comboBox.DisplayMember = "Description";
  comboBox.SelectedIndex = 0;
}    

BindPriceList(objInsuredPrice, GetPriceList());
BindPriceList(objTPPrice, GetPriceList());
BindPriceList(objProvSum, GetPriceList());