I have this code where I get the tree view of a table in access database..
what I want is ..I want to add nodes on each base on it's ID...

this is the table

ID    Name    ID_Menu
1      pet     
2      me       1

and the treeview's output will be
me will be a child node of Pet base on ID_Menu same as the ID...

        -  me

Please help just need for an exam....

Recommended Answers

All 2 Replies

I'll repost this in a few minutes. Just noticed something really dumb.

Sorry it took so long to respond. One thing led to another. OK. A quick lesson in TreeViews. Every tree must have a top (or root) node. There may be more than one node at the top but there must be at least one. A node can have sibling nodes (nodes at the same level of the tree) and child nodes (nodes one level deeper). Two of the properties of nodes are Name (the node's ID) and Text (the node's displayed value). The default value for Name is something like node## where ## is generated automatically when the node is created. If you make Name unique you can use the Find method to locate the node with that name either in one collection of nodes or in any node from a given point and under. For a tree like

        Black Lab
        Irish Setter
        Maine Coon

If you wanted to add a new breed of dog you could either traverse the tree yourself or, if you made Name the same as Text you could locate the correct sub-tree by

Dim nodes() as TreeNode = TreeView1.TopNode.Nodes.Find("Dogs",True)

Select Case nodes.Count
    Case 0
        'couldn't find "Dogs" node
    Case 1
        'found one node named "Dogs"
        Dim newnode as New Treenode("Golden Retriever")
        newnode.Name = newnode.Text
    Case Else
        'found more than one "Dogs" node
End Select

Find always returns an array of nodes, even if there is only one match. It's up to you to ensure the names are unique. If you don't want to use Finid then you can roll your own. You can iterate over a collection of nodes by "For Each" or by using the properties "Prev" and "Next". You'll need to use Prev and Next if you want to insert nodes in any kind of sorted order.

I haven't gotten into anything fancy with TreeViews and I have found that there is often more than one way to do something (frequently a better way than I use). Play around.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.