How to assign value to a dropdown box items?

  • Thread starter Thread starter Anil Gupte
  • Start date Start date
A

Anil Gupte

I am trying to use a dataset to assign a display name (one column from a
table) and also associate a value (2nd column) to each item in a dropdown
combobox. I tried it this way:

DropDownProfiles.DataSource = WizoProfilesDataSet
DropDownProfiles.ValueMember =
WizoProfilesDataSet.Tables("Profiles").Columns(0).ColumnName
DropDownProfiles.DisplayMember =
WizoProfilesDataSet.Tables("Profiles").Columns(1).ColumnName

And also this way
For RowIndex As Integer = 0 To
WizoProfilesDataSet.Tables("Profiles").Rows.Count - 1
DropDownProfiles.Items.Add(WizoProfilesDataSet.Tables("Profiles").Rows(RowIndex).Item(1))
DropDownProfiles.ValueMember =
WizoProfilesDataSet.Tables("Profiles").Rows(RowIndex).Item(0)
Next

But both don't work. What I really want to do is something like:
DropDownProfiles.items.item(rowindex).ValueMember =
WizoProfilesDataSet.Tables("Profiles").Rows(RowIndex).Item(0)
but there is no such member or property.

How does one do this?

Thanx,
 
Anil,

The first technique you show looks like it should work. What do you mean
when you say that it does not work?

Kerry Moorman
 
The control seeems to be frozen (shows a white area instead of the control)
and when I click it, it shows System.Data.DataViewManager....etc (the rest
is cut off) for the Display property instead of the database values like
"Kids", "Adults".

I tried it again because there seemed to be some confusion with the
ValueMember being a string not integer, and it is no longer frozen, but the
list is not populated with anything.

Thanx.
 
Also, what I am really looking for here is
WizoProfilesDataSet.Tables("Profiles").Columns(0).ColumnValue not ColumnName
and of course there is no property called ColumnValue.
 
Anil,

No, the named column will be used to provide values to the combobox's
ValueMember property. Which I believe is what you want.

But I still don't understand why your code is not populating the combobox. I
tried similar code and it worked fine for me. Perhaps you should create a
small test project that just assigns DisplayMember and ValueMember to a
combobox and see if you can narrow down the problem.

Kerry Moorman
 
OK! Solved it! I had two things wrong with it. First, I had some stuff
left over under DataBindings that I had manully added using the properties
window and that was no doubt conflicting with it. Secondly, I made a change
from:
DropDownProfiles.DataSource = WizoProfilesDataSet

to

DropDownProfiles.DataSource = WizoProfilesDataSet.Tables("Profiles")

and now it works - thanx for your help.
 
Back
Top