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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Freeman74Author Commented:
The example was clear and it worked perfectly.

Thanks a lot,

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.