Click here to Skip to main content
15,936,903 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?
Posted
Updated 12-Dec-10 23:41pm
v2

A helper method for binding data might be an idea here
C#
public static void BindField(ListControl control, object dataSource,
                         string displayMember, string valueMember )
{
    control.DataTextField       = displayMember;
    control.DataValueField      = valueMember;
    control.DataSource          = dataSource;
    control.DataBind();
}

You can then use as follows
C#
BindField(YourComboBoxControl, YourDataSource, "FieldNameYouWantToDisplayInComboBox", "IdYouWantToStoreInComboBox");
 
Share this answer
 
v2
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
 
v3

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