Hi I am adding comboboxes dynamically in a panel in windows form application. At run time, when I select any one of the comboboxes, all the comboboxes changes. i.e they dont retain individual selected item values.

here is my code.

for(int i=0;i<20;i++)

   {    Label l = new Label();
                l.Text = ColumnNames[i];
                l.Width = 250;
                cboColumnNames[i] = new ComboBox();
                cboColumnNames[i].Width = 250;
                cboColumnNames[i].DataSource = colname;
                cboColumnNames[i].Name ="Combo"+ i.ToString();
                l.Location = new Point(55, (i * 30) + 45);
                cboColumnNames[i].Location = new Point(750, (i * 30) + 45);
                cboColumnNames[i].SelectedIndexChanged += new EventHandler(ComboBoxSelectedIndexChanged);

How can I solve this issue?

3 Years
Discussion Span
Last Post by ss125

I'm not quit sure, but is it because they all using the same "ComboBoxSelectedIndexChanged"? What happens in that method?
You can also try using a separate datasource for each combobox.


As @C#Japp said, the problem is with the line

cboColumnNames[i].SelectedIndexChanged += new EventHandler(ComboBoxSelectedIndexChanged);

You are using a constant value to loop.

for(int i=0;i<20;i++)

So, you have to write the code for 20 comboboxes.

and to avoid the changes of other combos

you have to get the name or id of the combobox and write the function to do appropriate operation i.e only the particular combo value must be changed...

Hope this helps u...

Have a happy coding...:D

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.