p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   VB.NET 2002/2003 Basics (http://p2p.wrox.com/vb-net-2002-2003-basics-76/)
-   -   How do I select an ITEM from a combobox? (http://p2p.wrox.com/vb-net-2002-2003-basics/12875-how-do-i-select-item-combobox.html)

Ron Howerton May 11th, 2004 01:39 PM

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!

katsarosj May 11th, 2004 02:44 PM

I would be happy to hear otherwise, but I have done tons of searching on this subject and have found no other method but to use SelectedValue or SelectedIndex. That being said, if you want to loop through all of the items in a combobox you can do something like:

For x = 0 to combobox.items.count - 1
'add your code here (code from below would go here)
Next

In this example, I had a known value that I was comparing it against and I would add:

If combobox.items.item(x) = ItemValueIKnow Then
  combobox.selectedindex = x
End If

----------------------------
There may, and should be an easier way, but if there is I couldn't find it. Hope this helps.

J

Ron Howerton May 11th, 2004 02:57 PM

I wish your example would work, but it doesn't. I'm getting this error:

"Operator is not valid for type 'DataRowView' and string "Annual Leave"."

on this line:

If v_cboSearch.Items.Item(intPos) = v_strValue Then

I don't understand how Items.Item(x) references a DataRowView. Why doesn't it simply point to the DisplayValue? What am I doing wrong?!?!?

Imar May 11th, 2004 03:40 PM

Hi Ron,

There must be other ways to do this, but you could use FindStringExact to select the item, like this:

ComboBox1.SelectedIndex = ComboBox1.FindStringExact("YourString")

This will select the *first* item in the list that matches "YourString"

I think you get the error about DataRowView because that's the underlying datasource for the combobox.

I am not sure if this is the best way to preselect an item, but at least I got it working with this code......

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Vergeet Me by Doe Maar (Track 3 from the album: Doris Day en andere Stukken) What's This?

katsarosj May 11th, 2004 03:43 PM

In my example, I use a value pulled from a dataset row. That is the value that I evaluate against all of the items in the combobox.

The value for the combobox.items.item(x) is the value displayed in the combobox at that given index of x. So, if at index 2, I have "John Doe" displayed in the combobox, when x = 2, I am comparing "John Doe" against the value that I am looking for.

If it is equal, I know that this is the value I need so I set the SelectedIndex equal to x, then I exit the For loop. Obviously, the values would have to be unique or it would just select the first one it comes to.

Sorry I can't help you with your error, but maybe this explanation will help you out somehow.

J

Ron Howerton May 11th, 2004 04:14 PM

Both suggestions work wonderfully in a brand new project. Neither works in the one that's giving me fits, though. I must've set the "Produce Random Errors" option somewhere in the project and now I don't know how to unset it.

Thank you Microsoft for inventing new ways to shoot myself in the foot. :(

Imar May 11th, 2004 04:42 PM

This is really weird.

I tried it with "simple objects", an array of Integers and bound it to the Combo. Worked fine.
Then I bound the combo to a DataTable. set the ValueMember to a Guid in the table, and the DisplayMember to a varchar. Even then, the FindStringExact method worked fine.

How do you bind your data to the combo? What type is the datasource? And are you sure exact is really exact? That is, no spaces or other trimming issues are messing with your head? ;)

If this doesn't help, I suggest you uncheck "Produce Random Errors" on the properties dialog for the project.... ;)

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: All Mine by Portishead (Track 2 from the album: Portishead) What's This?

Ron Howerton May 11th, 2004 04:47 PM

I finally got it working, using the suggestion supplied by Imar. I believe the suggestion supplied by katsarosj would also work, but Imar's was shorter in both code and, I hope, processing time.

The crux of the problem, as far as I can tell, was that datarowview error, as suspected. I don't know why it kept think I was trying to reference such an object BUT I suspect it might've had something to do with trying to bind the combobox against a table in a dataset that contained other tables as well (even though no explicit or implicit relationships between the tables existed). The datarowview reference went away as soon as I created a new dataset for the table being bound to the combobox, and only placed the table I needed in that dataset. I'd never have guessed that was the problem (thanks to Microsoft for it's typically helpful error messages) but it was your suggestions that led me to try the processes in a simpler program, and the only difference I could find between the two was the complicated dataset in the program throwing the error.

Thanks to you both for your suggestions!

Imar May 11th, 2004 06:02 PM

Glad it's working now.

How did you bind our data? Did you assign the DataSet, a DefaultView or an explicit table?

Maybe you can use a more complex DataSet, but you'll need to use a strongly referenced table, like this:

MyCombo.DataSource = MyDataSet.Tables("MyTable")

That worked for me, although I didn't try a DataSet with multiple tables yet.

I think it's true what they say about the .NET Framework: "It makes the hardest things easy, and the easiest things very hard." I can't believe selecting an item in a ComboBox should be so hard....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: The Saxophone Song by Kate Bush (Track 2 from the album: The Kick Inside) What's This?

Ron Howerton May 12th, 2004 08:12 AM

The Combobox was bound to the dataset in pretty much the same way as you show in code, except done in the VS GUI Properties window. The dataset was created from an adapter dropped on the form from the server explorer for the table. The DisplayMember and ValueMember of the combobox were then bound to their respective fields, also in the VS GUI. I was then sharing that dataset with another table being bound to another combobox. Somehow this appeared to result in the combobox thinking it was bound to a datarowview instead of individual fields.

I tried different combinations of binding for the combobox to see if I screwed something up there. One way you bind the DataSource to the dataset only, and then reference both table and fieldname in the DisplayMember and ValueMember properites. Another way is to bind the source to the dataset and table, and then DM and VM properties to just the fields within the table. Niether way seemed to to work, though.

Using katsarosj's suggestion with a slight modification, I discovered I was able reference the DisplayMember field explicitly, as though looking at a datarow, so VB was certainly right in that respect. But I then couldn't reference the SelectedIndex which made that solution unusable for my purpose. But it was enough to show that VB was technically correct in the error being returned, although it was of no use in understanding or correcting the problem being encountered. I'd say your suggestion is a fairly simple solution - I just wish it was easier to find. In any case, it wouldn't work either until I simplified the dataset structure by including only the table bound to the combobox. It's a part of my collection of .NET tricks now, though. Thanks!


All times are GMT -4. The time now is 03:12 AM.

Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.