Two values for combo box

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Firstly, I'm using C# if that will make a difference to the answer...
Is there any way you can assign two values for a combol box like in the vb6
'old days', so you have say, a string 'UserName' displayed in the combo box,
but that returns an integer 'userID' for that selection?

If not, what would be the best way to impliment this? An array? I am wanting
to use the Datareader, holding two columns of data to populate the combo box.

Any ideas would be most appreciated
Many thanks in advance
Ant
 
Ant,

What you need to do is to create your own class that holds both members -
item's diaplay string and item's value then you need to override ToString
for that class to return the item's display string. When populate the
combobox it will uses ToString to to render the comobox's items. When
selection changes you can cast the selected item to your class and read the
items value.

If you don't have control on the type of the object you load in the combobox
or for some other reason you don't want to (or you can't) override ToString
you can use combobox's DisplayMemer and ValueMember properties. In this
properties set the names of the item type's properties that will provide the
display text and the value of the object respectively. Once you do this the
combobox will use the DisplayMember to render the items and you can read
SelectedValue property to get the value of the selected item.
 
Back
Top