Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 11th, 2004, 01:39 PM
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
  #2 (permalink)  
Old May 11th, 2004, 02:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

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
Reply With Quote
The Following User Says Thank You to katsarosj For This Useful Post:
Ron Howerton (February 22nd, 2013)
  #3 (permalink)  
Old May 11th, 2004, 02:57 PM
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

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?!?!?
Reply With Quote
  #4 (permalink)  
Old May 11th, 2004, 03:40 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,960
Thanks: 79
Thanked 1,559 Times in 1,536 Posts
Default

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?
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Ron Howerton (February 22nd, 2013)
  #5 (permalink)  
Old May 11th, 2004, 03:43 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

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
Reply With Quote
The Following User Says Thank You to katsarosj For This Useful Post:
Ron Howerton (February 22nd, 2013)
  #6 (permalink)  
Old May 11th, 2004, 04:14 PM
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

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. :(
Reply With Quote
  #7 (permalink)  
Old May 11th, 2004, 04:42 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,960
Thanks: 79
Thanked 1,559 Times in 1,536 Posts
Default

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?
Reply With Quote
  #8 (permalink)  
Old May 11th, 2004, 04:47 PM
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

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!
Reply With Quote
  #9 (permalink)  
Old May 11th, 2004, 06:02 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,960
Thanks: 79
Thanked 1,559 Times in 1,536 Posts
Default

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?
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Ron Howerton (February 22nd, 2013)
  #10 (permalink)  
Old May 12th, 2004, 08:12 AM
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

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!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Display data as per combobox item selected yogeshyl Excel VBA 0 July 28th, 2007 07:17 AM
how to select an item from listview jthadathilm16 VB How-To 1 April 24th, 2006 12:33 PM
comboBox - tooltip for each Item filipczako C# 0 November 29th, 2005 06:46 PM
dynamic select item? vickriz Javascript How-To 3 June 11th, 2003 10:47 PM
dynamic select item? vickriz Javascript 1 June 5th, 2003 04:21 PM



All times are GMT -4. The time now is 01:25 AM.


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