Populate TreeView with records from a database

  • Thread starter Thread starter Kevin Otte
  • Start date Start date
K

Kevin Otte

Hi everybody,

I have a database table here, containing the full paths of all
subdirectories of a certain directory.

example:
E:\Development\VB.NET\Mobile
E:\Development\VB.NET\Services\FileSystemWatcher
E:\Development\C#\
E:\Development\C++\MFC-Apps

How can I use this table to populate a TreeView recursively like in
the Explorer so that the first level node would be 'E:' containing
'Development' containing 'VB.NET', 'C#' etc.

I'm having serious problems finding a proper algorithm.

Thanks in advance.
 
Kevin,

I would iterate through all of the strings, parsing it apart by the path
separator ("\"). Once you do that, I would create a recursive function that
would take a node and the rest of the path that you want to find/add.

Basically, the function would cycle through the child nodes, looking for
the segment of the path at the top (think of it as a stack). If it finds
it, it pops the value off the top of the stack, and then passes the rest of
the stack to the function again, as well as the child node that it was found
in.

If it was not found, then it would create a new node, attach it to the
parent, and name it the same as the path segment, and then pop it off. The
call would be made as in the previous case, with the newly created node and
the rest of the path.

Hope this helps.
 
Back
Top