asp.net - Generate list of parents-childs (recursive relationship) from Dataset -
i'm trying build list (html) recursive relationship. data in dataset converted data table if it's easier.
i don't know what's best option achieve this. thinking using nested repeaters.
here's data:
__id__ | __name__ | __parent__ | __level__ 1 | patrick | | 1 2 | mark | | 1 3 | scott | 2 | 2 4 | jason | | 1 5 | julian | | 1 6 | john | 6 | 2 7 | steve | | 1 8 | george | 1 | 2 9 | robert | 1 | 2 10 | rodney | 8 | 3
here output want produce
- patrick [1] - george [8] - rodney [10] - robert [9] - mark [2] - scott [3] - julian [5] - john [6] - jason [4] - steve [7]
the easiest way write recursive method. way operates depend on whether want have method return entire tree-structured list, or output data reads it. if want output data read it, code might this:
private sub outputtree(data datatable, parentid string, indentationlevel integer) each row datarow in getchildrows(parentid) outputrow(row, indentationlevel) outputtree(data, row("id").tostring(), indentationlevel + 1) next end sub
the above code assumes implement method called getchildrows
returns list of rows contain given parent id. assumes have method called outputrow
outputs given row @ given indentation level.
then, call method this:
outputtree(mydatatable, nothing, 0)
if want build , return tree structure, arguably better approach, code might this:
private function buildtreenodes(data datatable, parentid string) list(of mytreenode) dim nodes list(ofnew mytreenode)() each row datarow in getchildrows(parentid) dim node new treenode() node.row = row node.children = buildtreenodes(data, row("id").tostring()) nodes.add(node) next return node end sub
the above code assumes have defined mytreenode
class this:
public class mytreenode public property row datarow public property children list(of mytreenode) end class
then call method this:
dim rootlevelnodes list(of mytreenode) = buildtreenodes(mydatatable, nothing)
Comments
Post a Comment