Binding To ComboBox

.netdev

Member
Joined
Jan 7, 2006
Messages
10
Programming Experience
Beginner
Hello guys,
Another question in vb.net
in my vb.net form that is connected to a database access:
On The form Instead of displaying the Employee ID field which is not very
meaningful to end users, I want that the Combo Box to display the employes name while storing its associated ID.

How can i procceed so? Anyone have a code sample to that?

Thanks Again!!!
 
Last edited by a moderator:
You need to bind your DataTable to the ComboBox. You assign the DataTable itself to the DataSource property, the name of the column containing the employee name to the DisplayMember property and the name of the column containing the ID to the ValueMember property:
VB.NET:
myComboBox.DataSource = myDataTable
myComboBox.DisplayMember = "Name"
myComboBox.ValueMember = "ID"
Then, whenever the user selects an employee's name from the ComboBox you can get the corresponding ID using the SelectedValue property.
 
combine them in your sql query like:

VB.NET:
me.MyCommand.CommandText = "SELECT ID, (FirstName + ' ' + LastName) AS FullName FROM Employees"

then bind combo like:

VB.NET:
[LEFT]myComboBox.DataSource = myDataTable
myComboBox.DisplayMember = "FullName"
myComboBox.ValueMember = "ID"[/LEFT]
 
Back
Top