Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have Created 10 Comboboxes(C0,C1,------,C9) Dynamically,
with Datasource,DisplayMember,ValueMember.


When i am going to Select a value From C0, The other C1----C9
Values are also getting effected.
how to make each Combobox as Individual Selection?

Here is MyCode:

C#
private void addcontrols(int num)
{
for (int n = 0; n < num - 1; n++)
{
      cb = new ComboBox();
      cb.Name = "cmbRelationshipDyn" + n;
      cb.Size = new Size(133, 21);
      cb.DropDownStyle = ComboBoxStyle.DropDownList;
      if (n == 0)
      {
      cb.Location = new Point(86, ((n + 1) * 5) + 0);
      }
      else
      {
      cb.Location = new Point(86, ((n) * 35) + 0);
      }
cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
      cb.DataSource = dtrel;
      cb.DisplayMember = "Relationship";
      cb.ValueMember = "ID";
      panel1.Controls.Add(cb);
}
}
void cb_SelectedIndexChanged(object sender, EventArgs e)
{

}



Any body's help would be really appreciated.

Regards,
Pawan.
Posted
Updated 23-Sep-10 0:08am
v3

Some sample code would help. but I think you linked them all to the same source. Normally you would for example select a name and the other controls are filled accordingly (if linked properly) because they are data aware and notice the change (in reality the source notices the change and updates all other controls). But you don't want that now, so have to set it up separately for each combobox.

Good luck!
 
Share this answer
 
Comments
Pawan Kiran 23-Sep-10 5:36am    
I need some more clarity on this. plz provide it.
E.F. Nijboer 23-Sep-10 7:14am    
They all use the same source.
cb.DataSource = dtrel;

Pawan Kiran 23-Sep-10 10:08am    
if i do like below everything is works fine.
for (int p = 0; p < dtrel.Rows.Count; p++)
{
cb.Items.Add(dtrel.Rows[p]["Relationship"].ToString());
}
//cb.DataSource = dtrel;
//cb.DisplayMember = "Relationship";
//cb.ValueMember = "ID";

but the only thing is ID is not available.

could you plz tell me the Differences between both the above cases(with
Datasource binding, With out Datasource binding).
E.F. Nijboer 23-Sep-10 11:43am    
Uncomment and try to create a dtrel datasource for each combobox and bind it.
It would be something like:
C0.datasource = dtrel0.datasource;
C0.DisplayMember = "Relationship";
C0.ValueMember = "ID";
C1.datasource = dtrel1.datasource;
C1.DisplayMember = "Relationship";
C1.ValueMember = "ID";
etc...
E.F. Nijboer 23-Sep-10 11:54am    
ps. to explain it some more you would have to look at the datasource (bindingsource).
link: http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource_members.aspx

The Current property is the selected value of the combobox that changed, or in the case of the other combobox controls, that you also bound to the same source, will change accordingly and select the Current. This means that you need a datasource for each combobox.
By getting affected - What do you mean?
I see that you have the same event handler for all the combo boxes. Also you are instantiating a single combo box object to handle all.
Try creating an array of combo box. But again, I am not sure what exactly you want and what do you mean by getting affected.
 
Share this answer
 
Comments
Pawan Kiran 23-Sep-10 5:57am    
in my method, if i pass 10 as input. it will create 8 Comboboxes..
i want to assign same Datasource to all these 8 Comboboxes,
when i select any value from combobox(C0), The other combos(C1,----,C7) should not change their Text to Combobox(C0) selectedText.

This is what the output i want.
Have any idea do let me know.
anshudutta 23-Sep-10 6:34am    
You have one event handler for handling events for all combo boxes. I think that is what is causing the problem. Try creating array of combo box. See if that works
Pawan Kiran 23-Sep-10 6:45am    
Thanks to your post.
After Creating Array of Combo box also problem is not resolved.
Pawan Kiran 23-Sep-10 8:12am    
I will appreciate you for your posts, but i haven't get my task completed yet. as soon as i solve my problem i will vote.
I think each individual combobox can be determined from sender argument of
cb_SelectedIndexChanged

Other than that, each individual element will work I think.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900