Results 1 to 10 of 10

Thread: TreeView ID

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    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.

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    Re: TreeView ID

    How do I do this?

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: TreeView ID

    But is there any way of identifying the key of the selected item in a treeview?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: TreeView ID

    Quote Originally Posted by keystone_paul View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: TreeView ID

    Quote Originally Posted by jmcilhinney View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: TreeView ID

    Quote Originally Posted by keystone_paul View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width