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!
tonelm54Asked:
Who is Participating?
 
Juan_BarreraConnect With a Mentor Commented:
Before trying that, wouldn't be better to convert your rows into XML and bond the TreeView to an XmlDataSource?

Anyway, you can use tv.FindNode("path") to check if a node exists, which may slove your problem.
0
 
Juan_BarreraCommented:
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

0
 
tonelm54Author Commented:
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!
0
 
tonelm54Author Commented:
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.
0
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.