开发者

TreeView how to create node programmatically

I need populate a TreeView control to display Hierarchy Data from a DataBase.

The Hierarchy Order has been dictated by a Column, in my DB called "CategoryNodeString" and "CategoryNodeLevel".

/ = Root

Example:

CategoryId   CategoryNodeString   CategoryNodeLevel
1            /                    0
2            /1/          开发者_运维百科        1
3            /2/                  1
4            /1/1/                2
5            /1/2/                2
6            /1/1/1               3

Can you provide me a sample of code to start? Thanks guys!


With this data source it is not easy to do build your tree view. The complexity because there is no parent-to-child relationship between nodes. The CategoryNodeString should read the Id of the parent CategoryId as follows:

CategoryId     ParentCategoryId   Node Display String
0              0                  Parent
1              0                  SubParent
2              0                  SubParent
3              1                  Child
4              2                  Child

From this data you can get the following structure:

0 (Parent)
+--1 (SubParent)
|  +--3 (Child)
+--2 (SubParent)
|  +--4 (Child)

and so forth. Building this tree can be done (semi) recursively with Linq-to-objects by examining the parentCategoryId of each node. Start by finding the lowest value of the parentCategoryId. Create that node (using System.Web.UI.TreeNode object). Then append to it all children who also share that parentCategoryId. Then repeat the process (hence the recursion) for each newly appended child.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜