Click here to Skip to main content
15,944,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
cboField1.DataBindings.Add("SelectedItem", ds,"Orders2");

I have a table named Orders2 and I want to bind the Column names to a combobox. Is the sample code above the correct way of binding data to a combobox?
Updated 12-Dec-10 23:41pm

That could be the right way but I haven't used the same anytime.

You could also try to bind it using this alternate way.
cboField1.DataSource = Orders2 ;
cboField1.DisplayMember = //Name of fields to be displayed.<br>
cboField1.ValueMember = //Name of field to be kept as value.</br>

Please vote and Accept Answer if it Helped.
Share this answer
A helper method for binding data might be an idea here
public static void BindField(ListControl control, object dataSource,
                         string displayMember, string valueMember )
    control.DataTextField       = displayMember;
    control.DataValueField      = valueMember;
    control.DataSource          = dataSource;

You can then use as follows
BindField(YourComboBoxControl, YourDataSource, "FieldNameYouWantToDisplayInComboBox", "IdYouWantToStoreInComboBox");
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