tags:

views:

49

answers:

4

I have a WinForm Application with a grid that contains a ComboBox on each row. All are binded to the same collection ( The collection might change behind that's why I don't want to have different collections for each Combo, also memory cost). The issue is that when I select some object in one combo it changes the selected object on every Combo.. Here is a code you can run and easily reproduce.

public Form1()
        {
           InitializeComponent();

            this.comboBox1 = new System.Windows.Forms.ComboBox();
            List<int> numList = new List<int>(){1,2,3,4};
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(33, 169);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(126, 21);
            this.comboBox1.TabIndex = 3;
            this.comboBox1.DataSource = numList;   // BINDING TO NUMLIST

            this.comboBox2 = new System.Windows.Forms.ComboBox();
            this.comboBox2.FormattingEnabled = true;
            this.comboBox2.Location = new System.Drawing.Point(243, 367);
            this.comboBox2.Name = "comboBox2";
            this.comboBox2.Size = new System.Drawing.Size(126, 21);
            this.comboBox2.TabIndex = 4;
            this.comboBox2.DataSource = numList; // BINDING TO NUMLIST ( THE SAME LIST

            this.Controls.Add(this.comboBox2);
            this.Controls.Add(this.comboBox1);
        }

Just make a form and paste the declaration of the ComboBox 1 and 2. Any Idea how can this be happening. I mean If it is a simple List it doesn't keeps track of selected object. What is happening behind the DataSource?

A: 

When you want to bind, use the linq ToList() method. This will create a new list though, so they will become unrelated.

Pierre-Alain Vigeant
Sorry it didn't worked.. it keeps happening the same... I think this is something is creating behind the DataSource that and makes all comboBoxes point to the same thing but I can tell...
jmayor
Sorry I though that you wanted to be able to edit your list and that it change the two list. I misread the part about the two changing at the same time.
Pierre-Alain Vigeant
+5  A: 

You need to use seperate lists, if you bind to the same lists, that is the expected behaviour.

leppie
Why would it be the default behaviour? Imagine I have 10K rows? would I need to have 10K lists? and besides if I bind a simple Generic Collection List it make it seems like if the list keeps track of the selected object.. and it is a simple list... I would undertstand this if it would be like and ObservableCollection or some kind of view Object.
jmayor
@jmayor - "The CurrencyManager is used to keep data-bound controls synchronized with each other (showing data from the same record). The CurrencyManager object does this by managing a collection of the bound data supplied by a data source."
Philip Wallace
+1  A: 

Read this: Data Binding in .NET / C# Windows Forms

You will find the behavior you are seeing as actually correct. It is the CurrencyManager that is the root cause.

Philip Wallace
+3  A: 

The currency-manager is shared whenever you use the same data-source reference. One trick is to set the binding-context per control:

ctrl.BindingContext = new BindingContext();

Another option is to use difference references, for example by abstracting through a different BindingSource for each control.

Marc Gravell
Thanks a Lot!!!!!!! It saves me a lot of time having to update each list separately. Also want to thanks all the other guys and it was and interesting article to read..
jmayor