Solved TreeView Add nodes

Posted on 2008-11-11
Last Modified: 2012-05-05
Good evening,
I know how to add nodes to a treeview in, 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
    LVL 20

    Expert Comment

    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

    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

    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

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now