I have a parent-child data in a datawindow from a table mytab(id, pid....) Then I want to use to data to create a tree in treeview.
I try to use recursive function, but have problem with datawindow as I use filter to change the data in datawindow.
here is my code:
of_addnode(treeviewitem node, rootrow):
int li_rows,li_newitem, i
treeviewitem li_tvitem
dw_1.SetFilter("pid = " + string(node.data))
dw_1.Filter( )
li_rows = dw_1.RowCount()
if li_rows = 0 then
return
end if
for i = 1 to li_rows
li_tvitem.level = node.level +1
li_tvitem.data = dw_1.GetItemNumber(i,"id")
li_tvitem.label = dw_1.GetItemString(i,"description")
li_tvitem.pictureindex = 1
li_tvitem.selectedpictureindex = 2
li_newitem = tv_1.insertitemsort (rootrow, li_tvitem) // insert a new node
of_addnode(li_tvitem,li_newitem)
dw_1.SetFilter("pid = " + string(node.data)) //restore data back to filter, problem here. tree will have duplicate item
dw_1.Filter( )
next
how to create a recursive function from one datasource datawindow for this case?
Here are some ideas... it sounds like you want to use the treeview control and I don't blame you as the treeview dataobject is not very refined because it shows the expand button even if there aren't any children and I haven't found an elegant way to deal with this.
One thing to consider, will manipulating the data on back-end help you? This works for using the treeview dataobject type or might help in making the recursion logic more simple.
This database view is in MySQL but could be developed for other databases. I snipped it at three levels of recursion and yes it was hard coded for six levels of recursion the max supported by this application. This works great if using a treeview dataobject control but you are stuck with the expand button problems when there are no children. This can also be useful for making recursion logic simpler by effectively giving you "path" to the item.
**DATABASE VIEW TO FLATTEN PARENT AND CHILD (HIERARCHY) RELATIONSHIPS **
BUILDING TREEVIEW USING RECURSION
You are on the right track for populating the treeview control but without seeing your dataobject it is impossible to know if your setfilter and filter are working properly and what is going on. The key is to get the level from the insertitemXXX function and pass that into the first argument of the next insertitemXXX function. This simple example of the PB help always gets me on the right track. I see you are using treeviewitems but that doesn't really change things. Hope this helps.