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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListBox and ID's from Database 1

Status
Not open for further replies.

Muhenned

Programmer
Dec 31, 2007
4
BA
Hi all,
maybe you have already such a topic, but I really don't have much time to browse the whole forum.
So my problem is...
I have a ComboBox on my Windows Form, and I fill it with data from a database with DataReader object. So my question is, when I select an item in my ComboBox, how can I get the ID of this item from the DataBase. Is there any function or property to do this?

Thanx
 
Not sure if you can bind to a DataReader (I don't use binding), but if you can, take a look at the SelectedItem and SelectedValue properties -- they reflect the DisplayMember and ValueMember properties that you set to point to the columns in your DataReader.

If you're not databinding, one way to do this is create a small helper class that overrides the ToString() method. For every row from your DataReader, create a new instance of this class and add it to the Items collection. Inside this helper class is whatever you want to be displayed (shown by the ToString() override) and the database key info. When the user selects an item from the combobox, the SelectedItem property returns this object to you, and you can then extract the key (or any other values you want -- you might have a multi-part key and you can store each part as a seperate value in the helper class).

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I don't use binding"

Chip - I love you...

The combobox is a bit tricky as it does not have ComboBoxItems like a listview. A listview has a set of ListViewItems which in turn have a Tag property. You can also easily create a dictionary with listviewitems as keys and the actual value as the pair. You can do the same thing with a drop down list where the key of a dictionary is the index of the drop down item and the actual value is still the pair. The only danger is if you change the order of the combobox items. The helper class is a better solution in this case.

 
This works because the Item in a ComboBox is of type Object, and thus can hold anything.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
It's me again:)

My question is the same, but now related to TreeView. The problem is, that the nodes of TreeView can only take an objects of type string, and not of type Object (as I could see), so the helper class I created for my listBox cannot be used here.

Need tips...

Thx!
 
If I remember correctly the Treeview tag can take an object
so you can do something like:
Code:
treeNode.Tag = myObject;
and get it back again:
Code:
MyObject myObject = (MyObject) node.Tag;
Hope this helps.



Age is a consequence of experience
 
Thx, glad i could help :p

Age is a consequence of experience
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top