Combobox - 2 values - ID and Name

With the standard VB.NET ComboBox is it possible to store two values within the drop down menu. What I want to do is store the ID and the Name within the ComboBox but only save the ID relating to the name to the database.  Then went the record is loading pickup the ID and match it with the Name within the drop down menu.

Is this possible? If so now?
Who is Participating?
Sure you can. BUT, you need to do with with databinding.  Create a DataTable object that has two fields: A display field and a Value field. Something like this:

' Create new DataTable object
        Dim dt As New DataTable

' Add a couple columns to it
        dt.Columns.Add("Display", GetType(String))
        dt.Columns.Add("Value", GetType(Integer))

' Array of names:
        Dim aNames As String() = {"Jim", "Joe", "Mary", "Cathy", "Jeff"}

         ' Add names to data table by building DataRows
         for ii as Integer = 0 to aNames.Count -1
             Dim row As DataRow = dt.NewRow
             row("Display") =  aNames(ii)
             row("Value") = ii

        ' Now bind to combo

        ComboBox1.DataSource = dt
        ComboBox1.DisplayMember = "Display"
        ComboBox1.ValueMember = "Value"

And you are good to go.

To ge the value of the selected item use:

You can create a custom string and work on it with Left and Right Functions.

As for example this string should reserve the first 4 characters for the id(to store up to 9999) ids. 5 chracters for 99999 and so on...

This is a little example: (example name is "Smith")

my string should be like this: 12   Smith
P.S. it is easy to store it reserving the chars...

If i want to get the ID then i should use a command like this
dim myID
myID = myComboBox.Items.Item(1)
myID = Left(myID, 5)
myID = lTrim(myID)
myID = rTrim(myID)
Now i have my ID number...

To get the name should be:
dim myNAME
myNAME = myComboBox.Items.Item(1)
myNAME = right(myNAME, (Len(myNAME) - 5))
Now i have my name.....

With a For Next function you can get all IDs
And with the instr() function you can search for IDs and/Or for Names!

Hope this help..

PeterErhardAuthor Commented:
Cheers for that.

I was hoping that you could have 2 fields within the combo box with 1 containing the ID which is hidden but you can get that value when a name is selected.

Is that not possible?
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

PeterErhardAuthor Commented:
Thanks for that ptakja.  I'm using the following and it works perfectly:

        Dim cmd As SqlCommand = cnn.CreateCommand
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "procLoadDisabilityOptions"

        Dim reader As SqlDataReader = cmd.ExecuteReader

        Dim dt As New DataTable()
        dt.Columns.Add("DisabilityIDAuto", GetType(Integer))
        dt.Columns.Add("Disability", GetType(String))

        While reader.Read
            With reader
                Dim row As DataRow = dt.NewRow
                row("Disability") = .GetValue(.GetOrdinal("Disability"))
                row("DisabilityIDAuto") = .GetValue(.GetOrdinal("LookUp_DisabilityIDAuto"))
            End With
        End While


        ComboBox_MainDisability.DataSource = dt
        ComboBox_MainDisability.DisplayMember = "Disability"
        ComboBox_MainDisability.ValueMember = "DisabilityIDAuto"

Is there a way once I have the ValueMember saved in the database that when loading the data I can go straight to the item within the drop down menu with the ID I have saved?
Use The following for selecting the value in the combobox

ComboBox1.SelectedValue = <id From Database>

What ChillAmmar said. The SelectedValue property is read/write. Meaning if you set it to something AND an existing item in the combo list collection contains that value it will be selected.
PeterErhardAuthor Commented:
Thanks - all works as expected!

One final question. I'm using the drop down list for the combo-box. At the moment it always defaults to the first option within the list. How can I make it blank to begin with so the user remembers to select an option from the list?
Try adding an item before data binding.

ComboBox1.Items.Add("<SELECT ITEM>")
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.