Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

vb.net TreeView Add nodes

Good evening,
I know how to add nodes to a treeview in vb.net, but need some help looking at an algorithm to add nodes.

In my navigation table I have the following rows:-
          Personal \ Banking\ Alliance and Leicester
          Personal \ Banking\ Barclays
          Personal \ Banking\ Natwest
          Personal \ Insurance \ Home \ Direct Line '98
          Personal \ Insurance \ Home \ Tescos '98
          Personal \ Insurance \ Home \ Direct Line '98
          Personal \ Insurance \ Car \ Tescos '99

Problem is, if I just add these nodes, I get 7 root nodes, instead of 1 root node, and 2 children etc.

Any ideas how I can write these nodes correctly?

Thanks in advance!
Avatar of Juan_Barrera
Juan_Barrera
Flag of New Zealand image

Hi there,

Here is an example:

    Dim tv As New TreeView
        Dim root As New TreeNode("Personal")
        tv.Nodes.Add(root)
        Dim child1 As New TreeNode("Banking")
        root.ChildNodes.Add(child1)
        Dim child2 As New TreeNode("Insurance")
        root.ChildNodes.Add(child2)

Open in new window

Avatar of tonelm54
tonelm54

ASKER

thats ok, but the amount of rows I have is in excess of 1000, and go several deep, so I need an algorithm to test if each node exists if it does move to child and add if doesnt exist.

So something like
writeNodes("Personal \ Insurance \ Home \ Tescos '98")

sub writeNodes(strNodePath as string)
     eachBranch = split(strNode,"\")
     for each eachBranch
          if nodeexists = false
               add node
          end if
     next eachBranch
end sub

Sorry for the phudeo code, but I dont know how to seach if a node exists, so if possible test weather the tree branch exists:-
Personal
     Insurance
          Home
               Direct Line '98

return true or false.

Sorry if this doesnt sound right!
ASKER CERTIFIED SOLUTION
Avatar of Juan_Barrera
Juan_Barrera
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yup I could use a data source, however looking into databinding the control it wont support different levels of children.

For example some branches can have children with children with children with children with children with children with children with children, and some branches just have 1 child.