Question Combobox displaymember

adarsh_fj

Member
Joined
Jul 14, 2010
Messages
5
Programming Experience
Beginner
Hi All, i am new to vb.net and hope i could get some help here. I want to display the product no and the product name both together in the combobox from a table using a dataset. So in the list i would see something like this " 01 Sugar " where 01 is product code and sugar is product name. I am using the following codes but i can only display either of the one. how can i display both field together.


Try
sqlconn.Open()
Dim x As String = "select * from products"
Dim sqladpt As New SqlDataAdapter(x, sqlconn)

Dim sqlds As New DataSet

sqladpt.Fill(sqlds, "Products")

With ComboBox1
.DataSource = sqlds.Tables("Products")
.DisplayMember = "Product Name"
.ValueMember = "Product No"
.SelectedIndex = 0
End With


thanks in advance
 
You can only display the contents of one column in a standard ComboBox so you have two choices:

1. Keep using a standard ComboBox and add an extra column to your DataTable that contains the combined values from the other two.
2. Replace your standard ComboBox with a custom multi-column ComboBox that you would be able to find on the web with a search.

If you choose option 1 then you have two more choices:

1. Change your SQL code to return an extra column in the result set that combines the values from the other two.
2. Add an extra DataColumn to your DataTable after populating it and set the Expression property such that it combines the values from the other two columns.

By the way, don't use a DataSet unless you're actually using a DataSet. You're only using one DataTable so just create one DataTable. Change this:
Dim sqlds As New DataSet

sqladpt.Fill(sqlds, "Products")

With ComboBox1
.DataSource = sqlds.Tables("Products")
with this:
Dim sqldt As New DataTable

sqladpt.Fill(sqldt)

With ComboBox1
.DataSource = sqldt
 
Back
Top