VB.NET 2005 Combobox, Multiple Values per line

Posted on 2007-08-01
Last Modified: 2013-11-26
I have Visual Studio 2005 and am coding in

On a form I have a combobox which is populated with values from a database.

In this particular example it is bound to the results from a dataset of room numbers. In the combobox once populated the values look like ,


etc etc.

Now is it possible to have in the combo box multiple information on each line. e.g. I want to have the room number and the type of room. eg.

501, Standard Double
502, Standard Twin
503, Suite

And then when ever one of these is selected from the drop down, I only want the LEFT hand side of the COMMA to display as the selected value, eg if i was to click on 502 from the dropdown, only '502' would display as the selectedtext value.

Any help hugely apreciated
Question by:superlative
    LVL 48

    Expert Comment

    Do you need to se the booth values or can you see just the description ?
    LVL 48

    Accepted Solution

    This way shows then text and you can get the text and the value

    Dim rdr As SqlDataReader = Nothing
    Dim strConn As String = "Data Source=" & SqlServerName & ";Initial Catalog=" & SqlServerDatabase & ";User ID=" & SqlServerLogin & ";Password=" & SqlServerPassword
    Dim conn As SqlConnection = New SqlConnection(strConn) 'Your connection

    Dim dt As DataTable
    Dim row As DataRow

    dt = New DataTable()

    Dim query As String = "SELECT * FROM table"
    Dim cmd As SqlCommand = New SqlCommand(query, conn)
    SqlReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

    While SqlReader.Read()
       row = dt.NewRow
       row(strFieldID) = CNull(SqlReader.Item(strFieldID))
       row(strFieldDescription) = SqlReader.Item(strFieldDescription)
    End While

    YourComboBox.DataSource = dt
    YourComboBox.ValueMember = strFieldID
    YourComboBox.DisplayMember = strFieldDescription

    Then use SelecteValue and SelectedText to retrive values
    LVL 18

    Expert Comment

    LVL 48

    Expert Comment

    If you want to show booth info you can use

    Author Comment

    Almost what I am after. The multicolumn will have to do. Kinda what i'm after.

    I have the following code which works;

            myCommand.Connection = conn
            myCommand.CommandText = "SELECT * FROM config_roomtype"

                myAdapter.SelectCommand = myCommand

                cboRoomType.DataSource = dt
                cboRoomType.DisplayMember = "type_shortcode"
                cboRoomType.ValueMember = "type_name"

            Catch myerror As MySqlException
                MessageBox.Show("Error Connecting to Database: " & myerror.Message)

            End Try

    However, what I mean is rather than have multicolumns, I wanted the selected value to show in the combo text field the short code, but if you click the down arrow it shows the long name.  So I assume there msut be some kind of way to change the combobox.text value when a long version name is selected from the combo's drop down, to then change the text value to be the shortcode version of that long name selected type.

    I don't know if that is clear enough?  Sorry guys.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    737 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

    18 Experts available now in Live!

    Get 1:1 Help Now