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

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,

Who is Participating?
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...
Freeman74Author Commented:
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.

Freeman74Author Commented:
The example was clear and it worked perfectly.

Thanks a lot,

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.