Connecting Tech Pros Worldwide Help | Site Map

Treeview

Newbie
 
Join Date: Nov 2008
Posts: 5
#1: Nov 21 '08
Hello Everybody,

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

Expand|Select|Wrap|Line Numbers
  1. Col.Add("C:\Root\")
  2. Col.Add("C:\Root\Child1\")
  3. Col.Add("C:\Root\Child1\GChild1\")
  4.  
  5. Col.Add("C:\Root2\")
  6. Col.Add("C:\Root2\Child1\")
  7. Col.Add("C:\Root2\Child1\GChild1\")
  8. Col.Add("C:\Root2\Child1\GChild1\GGChild1")
  9.  
Please help!
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#2: Nov 21 '08

re: Treeview


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.
Newbie
 
Join Date: Nov 2008
Posts: 5
#3: Nov 21 '08

re: Treeview


Quote:

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.

Expand|Select|Wrap|Line Numbers
  1. C:\Root\
  2. C:\Root\Child\
  3. C:\Root\Child\Grand Child 1\
  4. C:\Root\Child\Grand Child 2\
  5. C:\Root2\
  6. C:\Root2\Child1\
  7. C:\Root2\Child2\
  8.  
i wanted to add them in treeview dynamically because the collection will contains 100 to 200 entries.

The treview will look like this.

Expand|Select|Wrap|Line Numbers
  1. -C:\
  2.   | --Root
  3.   |   | --Child
  4.   |       | -- Grand Child 1
  5.   |       | -- Grand Child 2
  6.   | --Root2  
  7.   |   | --Child 1
  8.   |   | --Child 2
Please help me
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#4: Nov 21 '08

re: Treeview


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

Expand|Select|Wrap|Line Numbers
  1. Dim oRoot As TreeNode
  2. oRoot.Text = "C:\"
  3.  
  4. Dim oNode0 As New TreeNode
  5. oNode0.Text = "FirstFolder"
  6.  
  7. Dim oNode1 As New TreeNode
  8. oNode1.Text = "FirstChildFolder"
  9.  
  10. etc
  11.  
  12. oNode0.ChildNodes.Add(oNode1)
  13. oRoot.ChildNodes.Add(oNode0)
  14. 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...
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#5: Nov 21 '08

re: Treeview


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
Reply


Similar .NET Framework bytes