Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating TreeView from Category Tree Table

Status
Not open for further replies.

DanTheVBman

Programmer
Nov 9, 2000
10
0
0
GB
I have a table as follows :
ID, IDParent, CategoryName
How can I populate the tree efficiently from this table ?
Some sort of recursion was my guess but would apreciate some idea as how to go about it.
Thanks
 
You can populate the tree, step by step(level by level).

Wou can do something recursive.

Fist, you put the Root. Then, you put the nodes that have no other parent then the root. Then, you put the nodes that have as parents the nodes that you've already created.

In order to do that. You can use an array with 4 dimensions
1..3 the rows from the table, and the last one will have, for example 1-if the element is in the TreeView, and 0-if isn't.

And you parse the array and put those elements in the Tree that have the parents already in the Tree, and for those elemnts that you've put in the Tree, the 4-th dimensions of the array is 1..

You parse the array, until all the elements are 1.





 
Thanks, I hashed this together. Ok well this isnt what u suggested exactly but i use an array to track the ID's already added then cycle through the the array querying the database for records where the ID in the array = the IDP field.
Im not a great programmer but this works. BUT it makes a lot of record calls if the tree is big. Any further comments would be great. (copy it into notpad and it will look nicer.

Nodes = rsCats.RecordCount ' count total number of records / nodes
NodeFree = 1 ' next free position in array
NodeC = 0 ' current position to query against categoryIDP
NodeIDs(0) = 0 ' set the initial node to the root.
While Nodes > NodeC
rsCats.Close
sqlstr = "Select * From Categories where categoryidp = " & NodeIDs(NodeC)
rsCats.Open sqlstr, strConn, adOpenStatic, adLockBatchOptimistic, adCmdText
While Not rsCats.EOF
Elements = Elements + 1
Id = "ID" & rsCats.Fields("categoryid").Value
Idp = "ID" & rsCats.Fields("categoryidp").Value
Item = rsCats.Fields("categoryname").Value
Set tmpnode = TreeView1.Nodes.Add(Idp, tvwChild, Id, Item)
NodeIDs(NodeFree) = rsCats.Fields("categoryid").Value
NodeFree = NodeFree + 1
rsCats.MoveNext
Wend
NodeC = NodeC + 1 ' increment to check next node ID in array
Wend
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top