View Single Post
  #11 (permalink)  
Old May 12th, 2004, 12:43 PM
Imar's Avatar
Imar Imar is offline
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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)