Treeview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashiswa
    New Member
    • Nov 2008
    • 5

    Treeview

    Hello Everybody,

    I am facing problem to add the below collection to a treeview in vb.net.

    Code:
    Col.Add("C:\Root\")
    Col.Add("C:\Root\Child1\")
    Col.Add("C:\Root\Child1\GChild1\")
    
    Col.Add("C:\Root2\")
    Col.Add("C:\Root2\Child1\")
    Col.Add("C:\Root2\Child1\GChild1\")
    Col.Add("C:\Root2\Child1\GChild1\GGChild1")
    Please help!
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    And what is the problem you're facing? How can we help? What have you tried? Have you checked the MSDN website for the specification for TreeView and how to add nodes to it? Do you know how to iterate through folders on your system to create the nodes for your TreeView?

    We're not here to do your work or your research for you - we're here to assist you to do it yourself.

    Comment

    • ashiswa
      New Member
      • Nov 2008
      • 5

      #3
      Originally posted by balabaster
      And what is the problem you're facing? How can we help? What have you tried? Have you checked the MSDN website for the specification for TreeView and how to add nodes to it? Do you know how to iterate through folders on your system to create the nodes for your TreeView?

      We're not here to do your work or your research for you - we're here to assist you to do it yourself.
      Hi,

      Thanks for the reply.

      I know how to add nodes in treview control but the problem is how do i find the parent node before adding the child node.

      For example the collection object contains.

      Code:
      C:\Root\
      C:\Root\Child\
      C:\Root\Child\Grand Child 1\
      C:\Root\Child\Grand Child 2\
      C:\Root2\
      C:\Root2\Child1\
      C:\Root2\Child2\
      i wanted to add them in treeview dynamically because the collection will contains 100 to 200 entries.

      The treview will look like this.

      Code:
      -C:\
        | --Root
        |   | --Child
        |       | -- Grand Child 1
        |       | -- Grand Child 2
        | --Root2  
        |   | --Child 1
        |   | --Child 2
      Please help me
      Last edited by ashiswa; Nov 21 '08, 05:05 PM. Reason: double posting

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        There are a number of ways of doing this - it depends on which in your view is the most straightforward :

        Code:
        Dim oRoot As TreeNode
        oRoot.Text = "C:\"
        
        Dim oNode0 As New TreeNode
        oNode0.Text = "FirstFolder"
        
        Dim oNode1 As New TreeNode
        oNode1.Text = "FirstChildFolder"
        
        etc
        
        oNode0.ChildNodes.Add(oNode1)
        oRoot.ChildNodes.Add(oNode0)
        TreeView1.Nodes.Add(oRoot)
        Using this method, you need to keep track of the current parent node in order to add a child node, so you need to keep that in mind...if I were you, I'd use some kind of recursion to carry this task out. That way you don't have to code everything long hand. A single method will call itself to parse out each subtree.

        There are a couple of alternatives. One is to build an XML document that mirrors the data and use that as a datasource for your TreeView and the other is I think TreeView will also accept a hierarchical datatable:

        i.e. one that takes the form:
        • ItemKey (usually of type integer)
        • ParentKey (References ItemKey of another row in the table, of the same type as ItemKey)
        • ItemText (Text to be displayed on node)


        So if you create a table, create the relationship that links each child item to its parent, load the data table with the items, set the treeview's datasource as your table and databind...

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Please enclose your posted code in [CODE] [/CODE] tags (See How to Ask a Question). Code tags preserve indention and uses a monospaced font.

          This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

          Please use [CODE] [/CODE] tags in future.

          MODERATOR

          Comment

          Working...