Mike's post motivated me to try it without a ready-made dbtreeview. The attached demo uses a standard TTreeView and a few event handlers to display the database records in a tree.
When the demo runs for the first time it creates a dummy sqlite3 database containing the elements of paths and the corresponding IDs and ParentIDs as records. For example, the filename "c:\windows\fonts\Arial.ttf' is stored like this:
- At the top level there is the node for the drive "c:". It has ParentID -1 indicating that its parent is nil (root) - this is a convention within the program. The ID of the node is set to some arbitrary value, here 0.
- "windows" is a child of node "c:", i.e. its ParentID is the ID of "c:", 0, and its own ID is given some other value, 2.
- The next node "fonts" is a child of "windows" and has the ID of "windows" as ParentID (2) and gets some other ID value, here 3.
- "Arial.ttf", finally, is a child of "fonts" and has ParentID 3 and gets an ID 4.
The dataset stores also a number for the "FileType": 0 is a drive, 1 is a directory, 2 is a file. This value is used as the image index into a provided imagelist.
The method "PopulateNode" is called whenever a node expands, it has the expanding node as a parameter. Since the ID is stored in the Data field of each TreeNode the procedure determines the parentID from the node and uses it in a query to filter all records for which the ParentID field is the same as the ID of the expanding node. For all these records a node is added to the expanding node.
Whenever a node collapses the child nodes are deleted to keep the memory load of the program at minimum.