Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > how do I bind a dataset to a combobox?
 

how do I bind a dataset to a combobox?

I am writing a windows forms application using C#.  I have retrieved data from a vendor table into a dataset.  I want to display the "vendor_name" and have the "vendor_id" returned to me whenever the user selects a vendor in the combobox.

However, the following code does not work.


VendorDB db = new VendorDB();
DataSet ds = new DataSet();

ds = db.GetVendorInfo();

cmbVendors.Items.Clear();
cmbVendors.DataSource = ds;
cmbVendors.DisplayMember = "vendor_name";
cmbVendors.ValueMember = "vendor_id";


When I run the app with this code, I get the following error: "Could not bind to the new display member.  Parameter name: newDisplayMember."  If I comment out the line where I am setting the "cmbVendors.ValueMember" property, I don't get the error message.  However, the data is still not displayed.

What am I doing wrong?
MigrationUser 1  Friday, December 26, 2003 1:32 PM
yeah , because you havent told it what datatable to bind to..

your just saying , bind this dataset and these columns , you havent specified what table in the dataset you want..

//Roger
MigrationUser 1  Monday, December 29, 2003 9:21 AM
That is you should write:
cmbVendors.DataSource= ds.Tables["VENDORS"];

(replace VENDORS with the table name).
MigrationUser 1  Thursday, January 08, 2004 1:27 PM
i meet this problem too , but the  solution is very easy .
example:
Dim ldtb_temp as new datatable 
ldtb_temp =conn("select  country_name , country_id from country")
combobox1.datasource = ldtb_temp 
combobox1.displaymember="COUNTRY_NAME" 'Must upper case 
combobox1.valuemember = "COUNTRY_ID" ' Must upper case

that it ok .
MigrationUser 1  Wednesday, June 22, 2005 5:13 AM

You can use google to search for other answers

Custom Search

More Threads

• Combobox and enter key
• Binding to a UserControl in a DataGrid using BindingList
• Datagridview tooltip from database
• Binding
• Accessing Bound Data from within a DataGridView
• Problem with DataMember of DataGridView
• Expanding/Collasping rows in DataGridView
• Overriding Selection BackColor of a DataGrid cell
• Datagridview Control problem binding to columns
• fast loading datagrid and combo