32
votes

UPDATE: This is now resolved, see answer below.


On one of my forms (in a Windows Forms application) I have 3 Combo Boxes. These combo boxes need to display a list of prices (In text, with an integer back-end value).

All of these combo boxes are using the same data source (A List<> of type TSPrice, with ValueMember set to Price and DisplayMember set to Description).

My problem is this... Everytime I choose a price option from one of the dropdowns, they ALL change to the same value... Is this something to do with them all being bound to the same data source?

Here is how I am binding them:

var priceList = 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")
                    };

objInsuredPrice.DataSource = priceList;
objTPPrice.DataSource = priceList;
objProvSum.DataSource = priceList;

objInsuredPrice.ValueMember = "Price";
objTPPrice.ValueMember = "Price";
objProvSum.ValueMember = "Price";

objInsuredPrice.DisplayMember = "Description";
objTPPrice.DisplayMember = "Description";
objProvSum.DisplayMember = "Description";

objInsuredPrice.SelectedIndex = 0;
objTPPrice.SelectedIndex = 0;
objProvSum.SelectedIndex = 0;

//objInsuredPrice.DataSource      = objTPPrice.DataSource     = objProvSum.DataSource     = priceList;
//objInsuredPrice.ValueMember     = objTPPrice.ValueMember    = objProvSum.ValueMember    = "Price";
//objInsuredPrice.DisplayMember   = objTPPrice.DisplayMember  = objProvSum.DisplayMember  = "Description";
//objInsuredPrice.SelectedIndex   = objTPPrice.SelectedIndex  = objProvSum.SelectedIndex  = 0;

Edit: The issue was that they were all being bound to the same DataSource as confirmed by Saurabh. This is how I solved it.

var priceList = new List<TSPrice>
                    {
                        new TSPrice(0, ""),
                        new TSPrice(1, "Half Day"),
                        new TSPrice(2, "Full Day"),
                        new TSPrice(3, "1 + Half"),
                        new TSPrice(4, "2 Days"),
                        new TSPrice(5, "Formal Quote Required")
                    };

var insuredList = new TSPrice[5];
var TPList = new TSPrice[5];
var provList = new TSPrice[5];

priceList.CopyTo(insuredList);
priceList.CopyTo(TPList);
priceList.CopyTo(provList);

objInsuredPrice.DataSource = insuredList;
objTPPrice.DataSource = TPList;
objProvSum.DataSource = provList;

objInsuredPrice.ValueMember     = objTPPrice.ValueMember    = objProvSum.ValueMember    = "Price";
objInsuredPrice.DisplayMember   = objTPPrice.DisplayMember  = objProvSum.DisplayMember  = "Description";
objInsuredPrice.SelectedIndex   = objTPPrice.SelectedIndex  = objProvSum.SelectedIndex  = 0;
6
There isn't any. I haven't hooked up any events to these combo boxes. I literally dragged them from the ToolBox onto my form, gave them a name and put the above code in my forms "Load" event.Tom Glenn
This is an idiotic (and all that implies) implementation by Microsoft team. Does anybody believe it is correct behavior for component A to have any effect on component B because they consume the same input list? Does anybody think making a copy of the entire list for every component is a rational solution? Thank God this WinForms is dead technology. Now if they could only flush SilverLight down the drain ... oh, right, they did.Rick O'Shea
Yes, if you have multiple components that use a common list, it is normal that they affect each other. If you want separate lists, use separate lists.Suncat2000

6 Answers

74
votes

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

15
votes

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.

8
votes

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());
2
votes

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;
    }
2
votes

Beth Massi has written an article explaining this issue and the correct solution: https://web.archive.org/web/20190114100843/https://blogs.msdn.microsoft.com/bethmassi/2007/09/19/binding-multiple-combo-boxes-to-the-same-data-source/

She has a series of other videos on Databinding that she links to, also.

I've read the previous answers and can confirm that, sadly, none of them worked when I tried them.

Creating a new BindingContext on a combobox just seems to break it. I suggest doing as Beth explains: make a completely new BindingSource setup.

0
votes

This works for me and I don't need to copy the source.

List<string> days = GetDays();
List<string> months = GetMonths();
List<string> years = GetYears();

Son1DDLDay.DataSource = days;
Son1DDLDay.DataBind();
Son1DDLMonth.DataSource = months;
Son1DDLMonth.DataBind();
Son1DDLYear.DataSource = years;
Son1DDLYear.DataBind();

Son2DDLDay.DataSource = days;
Son2DDLDay.DataBind();
Son2DDLMonth.DataSource = months;
Son2DDLMonth.DataBind();
Son2DDLYear.DataSource = years;
Son2DDLYear.DataBind();