There are a number of ways of doing this - it depends on which in your view is the most straightforward:
- 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...