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
  #11 (permalink)  
Old May 12th, 2004, 12:43 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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

Hi Ron,

Interesting stuff, don't you think?

I think it makes sense that the Combo box thinks it is bound to a DataRowView, because I think it *is* bound to that kind of object.

The Items collection of a ComboBox is a System.Windows.Forms.ComboBox.ObjectCollection which means it can be bound to a collection of Objects. So you could create an ArrayList with your Widget objects that expose a Foo and a Bar property. You can bind the DisplayMember to Foo, and ValueMember to Bar (or the other way around :)) and at data-bind time, the run-time will ask the Widget objects for their Foo and Bar properties to fill the ComboBox.

With a DataTable, these individual objects are DataRowView objects, which, according to the documentation "Represent[s] a customized view of a DataRow exposed as a fully featured Windows Forms control".

So, at run-time, the Combo is bound to a collection of DataRowView objects. Under the hood, I think the framework knows how to call aDataRowView.Item("Foo") and aDataRowView.Item("Bar") for the Display and Value fields.

So, if you want to loop through the items, you'll need to cast them to the appropriate types, like this:
Code:
For Each MyItem As DataRowView In ComboBox1.Items
  MessageBox.Show(MyItem.Item("Foo").ToString())
  If MyItem.Item("Foo").ToString() = "A Name" Then
    ' ????
  End If
Next
As you can see, I am still unclear how to proceed at this point to preselect the item. I am not sure if the For Each has the same order as the actual drop-down (I think it does but I don't know if it's guaranteed), so I am not sure if keeping track of a counter to set the SelectedIndex to that counter does any good.
Also, a DataRowView item does not have a Selected property, so you can't select the item directly.

So, all in all, I am afraid you're still suck with your old solution. This post doesn't really solve your problem, but I hope it sheds some light on the DataRowView issue, and may actually trigger someone else to provide a more definitive answer to the situation.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Tumult by Stone Sour (Track 12 from the album: Stone Sour) What's This?
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Ron Howerton (February 22nd, 2013)
  #12 (permalink)  
Old May 12th, 2004, 12:49 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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

UPDATE: Inside the loop, you're dealing with DataRowViews. Since the SelectedItem expects an Object, you can just pass in the current DataRowView item like this:
Code:
For Each MyItem As DataRowView In ComboBox1.Items
  MessageBox.Show(MyItem.Item("Name").ToString())
  If MyItem.Item("Name").ToString() = "A Name" Then
    ComboBox1.SelectedItem = MyItem
    ' Item found and selected, Exit the loop
    Exit For
  End If
Next
Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Hymn Of The Big Wheel by Massive Attack (Track 9 from the album: Blue Lines) What's This?
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Ron Howerton (February 22nd, 2013)
  #13 (permalink)  
Old May 12th, 2004, 01:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Imar,

That is an excellent solution. In my example, I wasn't actually binding the combobox to anything. I had a pre-generated list of U.S. states, which was one of the fields in a database. I used it for an update form to determine which state was entered in the database, then selected it. Since the states were alphabetical and indexed at 0, it wasn't hard to set the SelectedIndex when a match was made.

I would definitely agree with Ron in saying that information on this subject was extremely hard to find. I came up with my solution about 3 months ago and spent much time Googling and looking through MSDN before I decided to go my route. Anyway, good forum and good job to both of you for shedding more light on this subject.

J
Reply With Quote
The Following User Says Thank You to katsarosj For This Useful Post:
Ron Howerton (February 22nd, 2013)
  #14 (permalink)  
Old May 12th, 2004, 02:00 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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

Oh, yeah. I agree with you as well. It *is* hard to find information about this. That's also what I meant with my ".NET makes the easy things difficult" remark.
It took me lots of Googling and debugging as well to come up with this solution.......

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Ron Howerton (February 22nd, 2013)
  #15 (permalink)  
Old May 12th, 2004, 02:04 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

Imar,

Your update makes sense and I think I see some of the light, anyway. Of course the combobox considers itself bound to a datarow, since there's a dataset underlying it. But it that's the case, why doesn't it think so any longer now that I've changed the dataset format? The same code that produced the error earlier no longer does even though it's still bound to a dataset containing the very same data. I'm also still puzzled why it thought SelectedIndex was a datarow in the nonworking code as well. I've never heard of an Index that wasn't numeric.

But in any case, this crisis has been averted. On to the next one. ;)

Thanks again for your suggestions as well as the interesting discussion. I won't forget this lesson anytime soon. :)
Reply With Quote
  #16 (permalink)  
Old May 12th, 2004, 03:00 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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

I don't know why the code doesn't work with a more complex DataSet. Right now, I can't see any reason why it would do so. I also don't know why the problem doesn't occur with a simple DataSet. Need to test this someday....

With the "nonworking code", are you referring to this:

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

It makes sense you get an error about the conversion. The Items.Item(intPos) is not Numeric, but refers to a DataRowView object located at position intPos. That is, the Item property (or the Items indexer in C#) returns an object of the underlying datatype, which happens to be a DataRowView in this case.

So, this would have worked:

If CType(v_cboSearch.Items.Item(intPos), DataRowView).Item("FirstName") = v_strValue Then

(assuming FirstName is the item you bound your data to).

Does this clarify things?

Maybe a complex DataSet exposes different information. Maybe when there is only one table, it knows exactly what is is dealing with and can expose default properties. Maybe with a complex DataSet, you need to set some properties explicitly yourself. Like I said, I'll test this someday and see if I can make any sense out of it.


Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Ron Howerton (February 22nd, 2013)
  #17 (permalink)  
Old May 12th, 2004, 03:10 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 was referring to:

combobox1.SelectedIndex = 1

I would expect a property ending in the word "Index" to expect a numeric value (or perhaps a string ala accessing tables by name from a dataset), but not a datarowview (which seemed to be the error the above statement generated with the ciomplex dataset). I mentioned it obliquely in reference to katsarosj's suggested solution - I could find the index of the value I was searching for, but was unable to set SelectedIndex due to this bizarre datarowview problem.

Since I've got this code working as desired now, it's mostly a matter of curiosity rather than any pressing need to understand. In the future, I plan to bind only simple datasets and avoid the problem altogether.
Reply With Quote
  #18 (permalink)  
Old May 12th, 2004, 03:43 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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

Quote:
quote:Since I've got this code working as desired now, it's mostly a matter of curiosity rather than any pressing need to understand.†
Yeah, same here. It's quite interesting to find out the underlying principles of these seemingly easy tasks. I often seem to pick up something useful that comes in handy somewhere down the road.

The SelectedIndex *is* strange. You should indeed be able to set the index with an Integer, as the property is of type Integer. Anything greater than -2 and less then the number of items -1 should be acceptable.

Oh, well, like you said: one for the "collection of .NET tricks"....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Ron Howerton (February 22nd, 2013)
  #19 (permalink)  
Old October 2nd, 2015, 09:18 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default pleas sent code

hi to all i need to finish my program ineed code of my voting system...how to count my vote im using combobox for the name of the candidates what is the code of automatic count perclick the name list
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 06:17 AM
how to select an item from listview jthadathilm16 VB How-To 1 April 24th, 2006 11:33 AM
comboBox - tooltip for each Item filipczako C# 0 November 29th, 2005 05:46 PM
dynamic select item? vickriz Javascript How-To 3 June 11th, 2003 09:47 PM
dynamic select item? vickriz Javascript 1 June 5th, 2003 03:21 PM



All times are GMT -4. The time now is 06:59 PM.


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