0

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!

2
  • Why have you posted the same question twice? stackoverflow.com/questions/4613607/… Commented Jan 6, 2011 at 15:14
  • Hi, here I assume no EF invoved. It is a similar more simple version. Do you have any idea? Thanks for yuor message Commented Jan 6, 2011 at 15:26

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I really apreciate you comment, at the moment i figured out a solution using EF 4 I will post the solution here as soon as i can thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.