2

I have an object which can hold children, so children of each object themselves have children. so imagine is a 3 level hierarchical tree. however this number is not fixed, it can be 4,5 or even 6 hierarchical levels. I already have a BindingList which is the list of all the Root nodes. Does anyone know an straight forward way to convert this into table? so each level will be one column of the hierarchy. It is simple to create the columns but I am not sure how to populate it without knowing how many loops I need?

Any suggestions is highly appreciated.

Cheers.

Zardaloop
  • 1,594
  • 5
  • 22
  • 43

3 Answers3

1

I think the best way to do that is a recursion. I don't know the structure of a BindingList, I will write the most general I can, to you adapt the right name of methods:

void visitNode(Node loc, int level)
{
    //write here the code to save the node loc in the column = 'level'

    foreach(Node n in Loc.children)
        visitNode(n, level + 1);
}

Where Node are the type of a child, and Loc.children is a list or a collection of children.

That recursion will visit all the nodes.

Conrado Costa
  • 434
  • 2
  • 12
1

If you are using SQL Server 2008, you can make use of the hierarchyid

You can also check out the following articles on SO

Community
  • 1
  • 1
Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
0

I have managed to sort this issue, by keep track of the end nodes, and then simply go back the hierarchy one by one. (So, technically, entering the last cell of the row and going back toward left.)

jpaugh
  • 6,634
  • 4
  • 38
  • 90
Zardaloop
  • 1,594
  • 5
  • 22
  • 43