Combobox - 2 values - ID and Name

Posted on 2004-10-28
Last Modified: 2010-04-23
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?
Question by:PeterErhard

    Expert Comment

    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..


    Author Comment

    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?
    LVL 14

    Accepted Solution

    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:


    Author Comment

    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?
    LVL 2

    Expert Comment

    Use The following for selecting the value in the combobox

    ComboBox1.SelectedValue = <id From Database>

    LVL 14

    Expert Comment

    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.

    Author Comment

    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?
    LVL 14

    Expert Comment

    Try adding an item before data binding.

    ComboBox1.Items.Add("<SELECT ITEM>")

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now