Listbox hidden value

Android

Active member
Joined
Mar 6, 2007
Messages
35
Programming Experience
1-3
Is it possbile to have a hidden value on a listbox item?
The reason I'm asking this is because the listbox is poulated with database results and when the user selects the item I need to ID of that item, at the minute I'm adding the listbox "ID| Item name"
When I need to get the ID i then use string split on SelectedItem.

Is there a better way of doing this because it looks messy on the form.
 
use the ListBox's SelectedValue property for this, you can also bind the SelectedValue to a dataset (database) just like you do with the DisplayMember property

SelectedValue is under 'DataBindings' in the Properties window, simply store the ID field there
 
is thre any way that I can set it without having a dataset? because I already have a function that reads the results of a query into hashtables so that i can easily select them
 
when you load the items in the listbox, simply set the SelectedValue property to the current item's ID
 
Android, you really should go the DataTable route.. you have put your things into hashtables so you can select them, but DataTable can be used like a hashtable can:

object myItem = myDatatable.FindByKey("primary key").Items("whatever column")


Plus, a DataTable, like Juggalo says, can be made the data source for the list. To know which item is selected by the user, you just call SelectedValue and you get the data row that is selected

If you really, really must persist with the route you have gone down, you should know that, the text shown in the list is whatever the .ToString() method of the object in the list collection returns.

So all your items in your collection, need to have an overridden ToString() method to return what you want showing in the list. Then all the items in the hash table .Values colelction need adding to the ListBox's .List colelction

Then, to make use of your item, you call SelectedValue, and that gets you the item itself. But then, I'm wondering what is the point of the hash table.. cause all you can do with it (if your object value also contaisn the key) is use your retrieved object to get the key to use in the hash table to retrieve.. oh, the same object again..

Which moots the need for the hashtable..


Consider scrapping the hashtable idea (theyre old anyway; you have 2.0, use Dictionary instead) entirely, have your data access layer return a datatable (not a dataset) which is a list-like, searchable container for data (oh.. like a hashtable? yes..) that can be bound to the list..

It will for sure make your life easier.. And if you think you have some other problem for which youre using HashTable, that the datatable cannot solve, then tell us, and we'll tell you how DataTable solves that problem (hopefully! :D )
 
Thanks for all that, got it working now.
is there any way that i can change the display member to a series of fields and add an extra string in there as well. before i was doing this by just concatenating it all into one string.
 
Assuming youre using a datatable implemntation, then yes.. the columns in a DataTable have a .Expression property that you can use (in loose sql style) to build up an expression. Then set your DisplayMember to this new column

If more advanced processing is required, it might jsut be possible to:
DOnt set DisplayMember to anything
Override ToString() for that particualr datarow (hopefully youre using Typed Datasets rather than Untyped ones) to include the new processing you require

I'm not certain, on this last point; would you like me to check it out for you?
 
yes, in your DataTable add a new Column with the appropriate header then when you fill the table simply supply your new column with the combined fields (concatenating)
 
yes, in your DataTable add a new Column with the appropriate header then when you fill the table simply supply your new column with the combined fields (concatenating)

Did you mean that after you fill it, go back over it and generate the column data yourself? Or did you mean to use the .Expression property?
 
Did you mean that after you fill it, go back over it and generate the column data yourself? Or did you mean to use the .Expression property?

i actually goofed, i meant use the concant in the SQL statement to auto-generate all the fields in the table, plus the new field holding the Concant values
 
i actually goofed, i meant use the concant in the SQL statement to auto-generate all the fields in the table, plus the new field holding the Concant values

Good idea.. the only shortfall being it wont recalculate in realtime if you change one of the values.. I think (hope) .Expression does! :)
 
Good idea.. the only shortfall being it wont recalculate in realtime if you change one of the values.. I think (hope) .Expression does! :)

that's true if they change the data directly in memory and not in the database
which is where the .Expression should pick up the changes

like i said i goofed, using the SQL statement only works when pulling the data from the source (most likely a database) whereas this is probably not the case here and using the .Expression property would work better
 
Back
Top