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?

Dani AI

Generated

Brief diagnosis: the observed behavior is caused by data binding, not the event handler or the loop itself. When multiple WinForms controls are bound to the same list object, they share a single CurrencyManager (via the container's BindingContext). That shared manager keeps one current position, so changing one ComboBox's selection updates the others. was right to look at the event handler for correctness, and 's loop comment is a red herring — the loop is fine; the DataSource reuse is the culprit.

Practical fixes (pick one):

  • Give each ComboBox its own BindingSource so each has an independent CurrencyManager.
  • Give each ComboBox its own BindingContext (new BindingContext()) before assigning DataSource.
  • Or bind each ComboBox to a separate copy of the list (new List<T>(sharedList)) if the list is static.

Example (use a BindingSource per control and an event handler that uses sender):

// create an independent BindingSource for a single ComboBox
var bs = new BindingSource { DataSource = colname }; // colname is the shared list
combo.DataSource = bs;

// event handler that only acts on the ComboBox that raised it
private void Combo_SelectedIndexChanged(object sender, EventArgs e)
{
    var cb = sender as ComboBox;
    if (cb == null) return;
    // inspect cb.SelectedItem / cb.SelectedValue or use cb.Tag to identify the row
}

Troubleshooting notes: if using lambdas inside the loop, capture a local copy of the index (e.g., int idx = i;) to avoid closure surprises. If the list contains objects, set DisplayMember/ValueMember correctly. BindingSource is the recommended, cleanest solution when the same underlying data must be shared but selections should remain independent.

Recommended Answers

All 2 Replies

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.