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

Posted on 2006-04-18
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
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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

    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 85

    Accepted Solution

    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

    The example was clear and it worked perfectly.

    Thanks a lot,


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now