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?