subject_x
subject_x

Reputation: 421

How do you track items in a tree when populated from a recordset or database query?

In my database I have a table with a guid and a name field. The guid is my primary key. I can query the table and populate the tree using the "Name" data field. My problem is that the name field is not unique so it's difficult to track which guid + name combination it is. For example, my treeview might look something like this:

-Cities in America
 |
 -Cities in Oregon
      |
      +Milwaukee
      +Salem
 |
 -Cities in Wisconsin
      |
      -Milwaukee
      -Madison

The city of Milwaukee in Oregon has a different guid than the one in Wisconsin. When a user clicks on either Milwaukee, how can I keep track of which one was selected? I'll need to know the guid of that selection so I can requery the database and grab other fields pertaining to that city.

Upvotes: 0

Views: 224

Answers (2)

Michael Shmalko
Michael Shmalko

Reputation: 710

You can use Tag property to store your GUID. See Control.Tag Property

Edited: For MFC CTreeCtrl see CTreeCtrl::SetItemData
It does store a 32bit values only, so to track your tags you'll have to create an index of all tags -- you can use vector to store tags and SetItemData to a pointer to a tag in the vector.

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490338

When you insert an item into the treeview, you can include an lParam. It's only a 32-bit item, so you can't store your GUID directly, but you could (for one example) create an array of GUIDs as you insert items into the tree, and store the index into the array in the lParam.

When the user clicks on an item, that will select the item and you'll receive a TVN_SELCHANGED message, which will contain (among other things) a pointer to a TVITEM, which will contain the lParam you inserted as part of the item. You can then use that to look up your GUID.

Upvotes: 3

Related Questions