Databind to Custom Class

  • Thread starter Thread starter Marty Cruise
  • Start date Start date
M

Marty Cruise

I have a custom collection which contains instances of a
custom class. With me so far?

OK, now I set the datasource of a combobox to the
collection, having overridden the ToString() function of
the custom class to return one of its text properties.
Life is good.

The first of two problems involves the combobox showing
the first item in the collection. To get around this,
I've added a dummy class with a null text property, and a
tag that equals 0. Question one: is there a better way
to have the combobox default to nothing following
databinding to my collection?

The second problem: After populating the combobox by
setting its datasource to the collection, I want to
specify the selected item (if one exists) by pointing it
to a class which has been populated on the side that is
not contained in the collection, but is identical to one
which it contains. I want to achieve:

cmbCustomers.selecteditem = myJob.Customer

Am I dreaming, or is there a way to do this without
having to implement a 'For i' loop with a lot of CType-
ing?
 
Hi,

For the first problem: Right after setting your combo box's datasource to
your custom collection, set your combo box's SelectIndex to -1. Note that
this will work only for the first time I think.

For the second problem: Assuming your Customer has a unique ID (such as a
string), you can inherit your custom selection from Hashtable (or a
collection that will take a key and return the item object), then this will
work.

cmbCustomers.SelectedItem = CType(cmbCustomers.DataSource,
MyCustomCollectionType).Item(myJob.Customer.UniqueID)

Hope it helps,

Huy Nguyen, Visual Basic team.

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
Back
Top