using Combobox displaymemeber and valuemember

Hello Experts,

I need to extract two fields from a database and bind one value as text to a combo box and the other as value to be called when the related text is selected.

so the code will look something like this:

while = true
           add as displaymember field 0, add as valuemember field1
end while

Thanks in advance, any help is greatly appreciated.
Who is Participating?
Use the code below to populate your combobox.

Public Function GetData(ByVal commandText As String) As DataTable
Dim connectionString As String = My.Settings.ConnectionString
Using connection As New SqlConnection(connectionString)
   Using command As New SqlCommand(commandText, connection)
       Using reader As New SqlDataReader = command.ExecuteReader()
            Dim table As New DataTable()
            Return table
       End Using
   End Using
End Using
End Function
Sample usage:
Dim dt As DataTable = GetData("Select Name, ID From Table1")
Me.ComboBox1.DataSource = dt
Me.ComboBox1.DisplayMember = "Name"
Me.ComboBox1.ValueMember = "ID"

Open in new window

pythonVAuthor Commented:
thanks, works like a charm.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.