vb.net TreeView Add nodes

Posted on 2008-11-11
Medium Priority
Last Modified: 2012-05-05
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!
Question by:tonelm54
  • 2
  • 2
LVL 20

Expert Comment

ID: 22934071
Hi there,

Here is an example:

    Dim tv As New TreeView
        Dim root As New TreeNode("Personal")
        Dim child1 As New TreeNode("Banking")
        Dim child2 As New TreeNode("Insurance")

Open in new window


Author Comment

ID: 22934843
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:-
               Direct Line '98

return true or false.

Sorry if this doesnt sound right!
LVL 20

Accepted Solution

Juan_Barrera earned 2000 total points
ID: 22934887
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.

Author Comment

ID: 22935504
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.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question