By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,699 Members | 1,372 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,699 IT Pros & Developers. It's quick & easy.

Databind to Custom Class

P: n/a
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?
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.Unique ID)

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.

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.