View Single Post
  #1 (permalink)  
Old May 11th, 2004, 12:39 PM
Ron Howerton Ron Howerton is offline
Friend of Wrox
Points: 2,702, Level: 21
Points: 2,702, Level: 21 Points: 2,702, Level: 21 Points: 2,702, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 425
Thanks: 57
Thanked 2 Times in 2 Posts
Default How do I select an ITEM from a combobox?

I have a bound combo box on a windows form. It has both DisplayMember and ValueMember set. I read a value from a database that matches a DisplayMember value (and NOT a ValueMember value). How can I select the item I want in the combo box so that when the form is displayed it will select the value from the database?

I can get it to work by setting SelectedValue. But I don't know the SelectedValue value, just the DisplayValue value.

I can get it to work using SelectedIndex. But I can't figure out the index for the value I want. I used to be able to iterate through the items list in a combobox and do a string compare against each displayed value and select the item I want, but it doesn't appear to be possible to iterate through combobox values any more.

And SelectedItem doesn't seem to do anything at all, despite the advertising. When I try to display SelectedItem after manually selecting an item, I get this error "Argument 'prompt' cannot be converted to string", which tells me that SelectedItem, whatever the heck it is, is not a string consisting of the value in DisplayValue for the combobox item I've selected. And when I try to set it it just comes back Nothing. I've found web references to combobox.selecteditem.value, but intellisense says the only method that exists for selecteditem is gettype.

This should be as simple as setting SelectedValue but I can't find anything to help. Is the only way to programmatically select a value from a combobox to set SelectedValue or SelectedIndex? Is there no way to iterate through the values in a combobox? Help!
Reply With Quote