Add a Child Node to a Treeview Based on the Tag Property

Posted on 2006-04-18
Medium Priority
Last Modified: 2008-03-17
I have created a Treeview and loaded the root nodes with a unique identifier using the tag property. Is there a way I can use the Unique Identifier in the Tag to assign Child Nodes.

Example I have the following

Root1          'Unique Identifier 100
Root2          'Unique Identifier 200
Root3          'Unique Identifier 300

I want to add the following

Child1, Child2 to Root1
Child3 to Root 2
Child4, Child 5 to Root3

The end result should be


I am storing the data for the treeview in an access table that uses the Unique Identifier as the Primary Key.

Unique Identifier, Node Name, Parent Node
     100             , Root1         ,  None
     200             , Root2         ,  None
     300             , Root3         ,  None
     101             , Child1        ,  100
     102             , Child2        ,  100
     201             , Child3        ,  200
     301             , Child4        ,  300
     302             , Child5        ,  300

The dataset is simplified, but I think it represents the concept.

Any help would be greatly appricated,

Question by:Freeman74
  • 2
  • 2
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16482574
When you create the nodes and add them to the TreeView, also add them to a HashTable using the "Unique Identifier" as the key.  Then you can quickly retrieve the correct node based on the "Parent Node" field in your table and add the newly created node directly to it without having to iterate thru the TreeView to find it.  When you are done building the TreeView you can discard the HashTable if you like...

Author Comment

ID: 16482720
Thank you for your response. I am very new to VB and have never used a HashTable before.

Are you saying that I should create a HashTable that stores the Unique Idenifier and the Node index to use as a cross-reference to find the parent node?

Also its 5:30 here and I'm about to head home. I will probably not be able to follow up anymore until tomorrow morning.

Again, Thanks for your time.

LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 16483347
Here is an example.  I've use ad string as my database for simplicity.  The code assumes that each "parent node" has already been added to the TreeView which means your records must be "in order"...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim node As TreeNode
        Dim parentNode As TreeNode
        Dim nodes As New Hashtable

        Dim data As String = _
            "100,Root1,None|" & _
            "200,Root2,None|" & _
            "300,Root3,None|" & _
            "101,Child1,100|" & _
            "102,Child2,100|" & _
            "201,Child3,200|" & _
            "301,Child4,300|" & _
        Dim record As String
        Dim records() As String = data.Split("|".ToCharArray)

        Dim values() As String
        For Each record In records
            values = record.Split(",".ToCharArray)
            node = New TreeNode(values(1))
            node.Tag = values(0)
            nodes.Add(node.Tag, node)
            If values(2) <> "None" Then
                parentNode = nodes(values(2))
            End If
    End Sub

Author Comment

ID: 16487459
The example was clear and it worked perfectly.

Thanks a lot,


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question