Populating Combo box from database

shepmom

Member
Joined
Aug 18, 2010
Messages
9
Programming Experience
Beginner
I am populating a combo box using data from a database, it is working correctly, however I am currently populating with the ID and I'd also like the description to be displayed.

I don't want to simply want to populate it with the description because I need the ID to be what is saved back when changes are made. How can I display additional data in the combo box?

Here is my code:

VB.NET:
Do While drReader.Read
            cboTheater.Items.Add(drReader("TheaterID"))
 
Populate a DataTable and then bind the DataTable to your ComboBox:
VB.NET:
myComboBox.DisplayMember = "Description"
myComboBox.ValueMember = "ID"
myComboBox.DataSource = myDataTable
That will display the contents of the Description column from the DataTable but, when the user makes a selection, you can get the corresponding value from the ID column via the SelectedValue property of the ComboBox.

How you populate the DataTable is up to you. You already have a DataReader so you might like to make use of it by creating a DataTable and calling Load. Alternatively, you could switch to using a DataAdapter.
 
Back
Top