• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

VB.NET 2005 Combobox, Multiple Values per line

I have Visual Studio 2005 and am coding in vb.net.

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 ,

501
502
503
504
505

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
0
superlative
Asked:
superlative
  • 3
1 Solution
 
jpaulinoCommented:
Do you need to se the booth values or can you see just the description ?
0
 
jpaulinoCommented:
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()
dt.Columns.Add(strFieldID)
dt.Columns.Add(strFieldDescription)

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)
   dt.Rows.Add(row)
End While
SqlReader.Close()

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


Then use SelecteValue and SelectedText to retrive values
0
 
Priest04Commented:
0
 
jpaulinoCommented:
If you want to show booth info you can use http://www.codeproject.com/cs/combobox/multicolumncombo.asp
0
 
superlativeAuthor Commented:
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"

        Try
            conn.Open()
            myCommand.ExecuteNonQuery()
            myAdapter.SelectCommand = myCommand
            myAdapter.Fill(dt)

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

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

        Finally
            conn.Dispose()
        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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now