I have never used TreeViews in the past and I want to display a hierarchical structure (recursive relationship of n levels). The data (available in a dataset - retrieved from database query) is in the following structure:
__ID__ | __NAME__ | __PARENT__
1 | Patrick |
2 | Mark |
3 | Scott | 2
4 | Jason |
5 | Julian |
6 | John | 6
7 | Steve |
8 | George | 1
9 | Robert | 1
10 | Rodney | 8
I'm trying to produce the following output
- Patrick [1]
- George [8]
- Rodney [10]
- Robert [9]
- Mark [2]
- Scott [3]
- Julian [5]
- John [6]
- Jason [4]
- Steve [7]
I'm trying to generate the Treeview control but have no experience with Treeviews. Any feedback or examples on how to achieve this would be really appreciated.
The big deal here is coming with a generic and adaptable enough algorithm capable of performing the required sorting. Once this is in place, writing the values into the
TreeView
is straightforward, just addingNodes
andChildNodes
.This code relies on two arrays (
NAME
(strings), which also includes [original position] andPARENT
(integers)) and performs the inclusions until the "second level", that is, main nodes and first child nodes.I guess that you will have enough information to understand how to deal with
TreeView
and to build an algorithm capable of performing the sorting you want.To fill the TreeView from a DataTable, try the following code
Code for VB.NET WindowsForms application
Code for ASP.NET application