tags:

views:

61

answers:

4

I fill the combobox with the below method and when I change the selection it's changed in all the Comboboxes why?

PowerProjectDBLinqDataContext dataContext =new PowerProjectDBLinqDataContext();
IEnumerable<Winding_Building_typeCombo> ls = dataContext.Winding_Building_typeCombos.ToList();
ComboBox cbx;
           for (int i = 1; i <= windingCount; i++)
           {
               cbx=((ComboBox)WindingPanel.Controls["winding" + i].Controls["cbxWindingBildingType" + i]);
               cbx.ValueMember = "id";
               cbx.DisplayMember = "value";
               cbx.DataSource = ls;
           }
A: 

I suspect that you have only one instance of cbx control. So each time you are binding your cbx to a data source you are actually overwriting currently existing data binding, and eventually your control will be bound to the last data source only.

Bashir Magomedov
the Combobox are all well fill and in plus I try to put "cbx=new Combobox()" in the For but it not work
Mario
Please provide us with code that shows creation of the object and adding it to the parent control.
Bashir Magomedov
I created the object visually drag and drop in the visual studio
Mario
A: 

Its clear why, u set just one binding source and every change u made on binding source (change the selection) effects all combos.

SaeedAlg
+1  A: 

the answer is probably not in this code.;

I imagine its because all iof the combo boxes are using the same reference.

you have probably done something like this

var combo = new ComboBox();
ComboBox cb1 = combo;
ComboBox cb2 = combo;
ComboBox cb3 = combo;

Edit: oops yeah as the other guy said, you are setting them all the use the same data context. Ie when you change the selected value in the datacontext they will all update to reflect their context - ie to select the same row.

cbx.DataSource = ls; 

this line is setting them all to the same thing. you need to take a copy of the datacontext in each case so that each combo points to a unique datacontext.

Try this

PowerProjectDBLinqDataContext dataContext =new PowerProjectDBLinqDataContext(); 
ComboBox cbx; 
           for (int i = 1; i <= windingCount; i++) 
           { 
               IEnumerable<Winding_Building_typeCombo> ls = dataContext.Winding_Building_typeCombos.ToList(); 
               cbx=((ComboBox)WindingPanel.Controls["winding" + i].Controls["cbxWindingBildingType" + i]); 
               cbx.ValueMember = "id"; 
               cbx.DisplayMember = "value"; 
               cbx.DataSource = ls; 
           } 
John Nicholas
this will cost me time and the memory maybe will be full because I have 10 combobox and a lot of data
Mario
premature optomisation is the root of all evil.
John Nicholas
How about you get it to work before you worry about how it runs? you will have to do some work. all i am doing is answering your question. Implement sopmething in the winding to do do a memberwise clone on the data to copy in it.
John Nicholas
A: 

I tried this method and it's working I create an image of the list in the place of the Context to save time and memory


PowerProjectDBLinqDataContext dataContext =new PowerProjectDBLinqDataContext();
            IEnumerable<Winding_Building_typeCombo> ls = dataContext.Winding_Building_typeCombos.ToList();
            ComboBox cbx;
           for (int i = 1; i <= windingCount; i++)
           {
               IEnumerable<Winding_Building_typeCombo> lsCopy = new List<Winding_Building_typeCombo>(ls);
               cbx=((ComboBox)WindingPanel.Controls["winding" + i].Controls["cbxWindingBildingType" + i]);
               cbx.DataSource = lsCopy;
               lsCopy = null;
               cbx.ValueMember = "id";
               cbx.DisplayMember = "value";
           }

Mario