Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ItemData equiv in .NET ComboBox 2

Status
Not open for further replies.

woogoo

Programmer
Feb 14, 2004
247
GB
Hi in previous versions of VB (i.e. v6) when you manually added items to a ComboBox you could set a property called ItemData. This was useful as it could be used to store a Record No. when the data was pulled from a DB.

However, I'm having no success trying to do the same with VB.NET 2005. All the references I've found refer to using a bound dataset, which due to the preprocessing required I can't use.

Does any one know who this can be done, I'd be most appreciative.
 
You can create a DataTable with the fields you want, loop through the data and add it to the DataTable (instead of to the ComboBox), then bind the ComboBox to the DataTable.

However, I do not see why preprocessing - whether a large or small amount - would prohibit binding a DataTable (either standalone or in a DataSet) to a ComboBox. Even if you have to do a lot of preprocessing, your data have to at some point be in some form that you are able to loop through them in order to add them to the ComboBox. There has to be some point at which preprocessing ends - otherwise it's not preprocessing, it's continuous processing - when you can then bind the DataTable to the ComboBox.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
There's no need for ItemData in .NET combo and listboxes. If you look at the datatype for the Item property, you'll see that it's object, which means that it can hold anything.

So, if you need to store extra data with an item, you'd create a helper class that would contain the text you want displayed, as well as your additional info (database keys, etc). You'd override the ToString() method to return your text, as that's what the framework will call to show your helper class in the combobox.

So, for every item in the combobox, you'd create a separate instance of your helper class, load it up with your data, then add it to the combobox, listbox, etc., and you're done.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
The equivalent of itemdata in combo boxes is the property "ValueMember".
For example assuming you have the following objects:
1. ComboBox named cboItems
2. DataTable "dtItems" having the fields(columns) "ItemID" and "Item"
Bind cboItems to the data source dtItems and set the following properties:
DisplayMember = "Item"
ValueMember = "ItemID"
At the run time the combo will display a list of the items from the database.
You can retrieve the "ItemID" by referring to the "SelectedValue" property of the combobox i.e. cboItem.SelectedValue.

I am not a good teacher but i hope that helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top