|
-
Mar 28th, 2009, 04:22 AM
#1
Thread Starter
Frenzied Member
TreeView ID
I want to select a node from my tree view, whatever I select is going to get a record from my database, I need a unique ID in each node so it will collect the correct document relating to the selected node.
How can I do this? the name of the node maybe used more than once? Each row in my database has an ID and Title, the title is the name I guess the ID being unique needs to be tied in somehow.
-
Mar 28th, 2009, 04:51 AM
#2
Re: TreeView ID
You could always use the tag property of the TreeNode object, ie set the node's tag to be the unique ID from the database. Then when the user changes the selection in the treeview you just query the tag of the selected node to retrieve the ID which you can then use to identify the correct record in the database.
-
Mar 28th, 2009, 05:41 AM
#3
Thread Starter
Frenzied Member
-
Mar 28th, 2009, 07:54 AM
#4
Re: TreeView ID
Tag is a property just like Text, so when adding nodes to the treeview you'd do something along the lines of :
Code:
NewNode.Text = RecordName
NewNode.Tag = RecordID
Then when you want to retrieve it the ID
Code:
RecordID = TreeView.SelectedNode.Tag
-
Mar 28th, 2009, 10:16 AM
#5
Re: TreeView ID
When you add a TreeNode to the Nodes collection of a TreeView or another TreeNode you can specify a key. You can then use that key to retrieve the node form the collection again instead of using a numeric index.
-
Mar 28th, 2009, 10:30 AM
#6
Re: TreeView ID
But is there any way of identifying the key of the selected item in a treeview?
-
Mar 28th, 2009, 10:33 AM
#7
Re: TreeView ID
 Originally Posted by keystone_paul
But is there any way of identifying the key of the selected item in a treeview?
No, the key is used to find the item, not the other way around. I was under the impression that you would have the ID and you wanted to find the corresponding node. If that's not the case then my suggestion is of no use to you.
-
Mar 28th, 2009, 10:39 AM
#8
Re: TreeView ID
 Originally Posted by jmcilhinney
No, the key is used to find the item, not the other way around. I was under the impression that you would have the ID and you wanted to find the corresponding node. If that's not the case then my suggestion is of no use to you.
Let me qualify that by adding that it actually IS possible but it's not simple. The point of the key is to find the item, so no simple means is provided to go the other way.
-
Mar 28th, 2009, 02:06 PM
#9
Re: TreeView ID
Should point out - it wasn't my original post, however my interpretation of the question was that kiwis was wanting to populate a treeview from a database and then subsequently re-located the item from the database based upon a unique ID stored with the associated treeview node.
It is something that always irked me though - that you if you store your unique database ID in the ID of the treenode you seemingly can't easily retrieve that ID subsequently. It seems to me that what kiwis wants (if I read it correctly) is eminently reasonable and it just strikes me as odd you can't really use the node's ID in the obvious way.
-
Mar 28th, 2009, 08:08 PM
#10
Re: TreeView ID
 Originally Posted by keystone_paul
Should point out - it wasn't my original post, however my interpretation of the question was that kiwis was wanting to populate a treeview from a database and then subsequently re-located the item from the database based upon a unique ID stored with the associated treeview node.
It is something that always irked me though - that you if you store your unique database ID in the ID of the treenode you seemingly can't easily retrieve that ID subsequently. It seems to me that what kiwis wants (if I read it correctly) is eminently reasonable and it just strikes me as odd you can't really use the node's ID in the obvious way.
Ah, didn't really look to see who was posting. Just assumed. 
It's worth noting that the key used when adding a TreeNode isn't really an ID. It's not a property of the TreeNode itself, but just a value used to identify it within the collection of nodes. In that sense the TreeNodeCollection is much like a Hastable or Dictionary. The whole point is that the key is hashed and can then be used to quickly find the corresponding value. As such there's no easy way to get a key by its value because a value is actually stored with a hash of the key, not the key itself.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|