Hello,
Has anyone here ever used recursion to take the results of ONE sql =
query, and fill a tree object accordingly. Here is my situation. I'm =
storing data on "templates", and certain templates can fall under other =
templates. From a data standpoint in sql server, I have a [parent] =
column, that contains the id of its parent (the primary key of the =
table), if this parent is "0", I'm assuming its parent will be my root =
node.
My strategy for doing this was to have one datatable (the results of the =
query), and then set dataviews in the recursive function to look at the =
desired [parent], if one exists, add that node to the tree, then call =
the same function again with the new parent. Here is my code I have:
'dt is my datatable, it is filled before this
Dim node As TreeNode =3D New TreeNode
node.Text =3D "All Templates"
node.NodeData =3D "0"
FillTemplates(dt, "0", node)
tv.Nodes.Add(node)
...
Private Sub FillTemplates(ByRef dt As DataTable, ByVal parent As =
String, ByRef node As TreeNode)
Dim dv As DataView =3D dt.DefaultView
dv.RowFilter =3D "[parent] =3D '" & parent & "'"
Dim i As Integer, count As Integer
count =3D dv.Count - 1
For i =3D 0 To count
Dim newNode As TreeNode =3D New TreeNode
newNode.Text =3D CStr(dv.Item(i)("label"))
newNode.NodeData =3D CStr(dv.Item(i)("tmpID"))
FillTemplates(dt, newNode.NodeData, newNode)
node.Nodes.Add(newNode)
Next
End Sub
....
This works..., until I have a nested depth of 3, then it breaks (the =
dataviews are getting messed up from prior recursive calls). I'm not =
really after fixing this function just yet, I'm throwing this question =
out in hopes someone may offer an entirely different way of going about =
this.
Anyone ever know of an easier way?
--Michael