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);
                panel1.Controls.Add(l);
                cboColumnNames[i].Location = new Point(750, (i * 30) + 45);
                panel1.Controls.Add(cboColumnNames[i]);
                cboColumnNames[i].SelectedIndexChanged += new EventHandler(ComboBoxSelectedIndexChanged);
  }

How can I solve this issue?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.