Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ComboBox Question

Status
Not open for further replies.

mlager

Programmer
Sep 1, 2006
74
0
0
US
I have a simple combobox that I don't want to bind to a data table. I just want to add a few items to it.

combobox_status.Items.Add("Active")
combobox_status.Items.Add("Inactive")

I want the user to see the descriptions above, but when selected, I want to be able to use a value instead of the description, such as A and I.

I know how to do this when bound to a table, but can I assign a display member and value member when not bound to a data table?
 

Unfortunately, you can't do this without binding to s DataTable. However you can build a DataTable in memory with the data you want and assign that to the ComboBox.


Dim dt As DataTable
Dim dc1 As DataColumn
dim dc2 As DataColumn

dc1 = New DataColumn("Status")
dc2 = New DataColumn("StatusCode")

dt = New Datatable

dt.Columns.Add(dc1)
dt.Columns.Add(dc2)

Dim dr As DataRow

dr = dt.NewRow()

dr.Item("Status") = "Active"
dr.Item("StatusCode") = 1

dt.Rows.Add(dr)

dr = dt.NewRow()

dr.Item("Status") = "Inactive"
dr.Item("StatusCode") = 2

dt.Rows.Add(dr)

combobox_status.DisplayMember = "Status"
combobox_status.ValueMember = "StatusCode"
combobox_status.DataSource = dt



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Your the best. Never thought about this approach. Appreciate your help!
 

Another - not as 'elegant' as jebenson's way - would be to just read first character of the Text property of your Combo box. You will get 'A'(ctive) or 'I'(nactive) this way, too.

Have fun.

---- Andy
 
You can also add the column to your table that has the 'A' or 'I' and then preset the ValueMember to that column.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top