D
David Veeneman
What's the best way to load a TreeView control from a database?
Here is my scenario: I have a TreeView that represents a Table of Contents.
The TOC has an arbitrary number of branches, and each branch has an
arbitrary depth. I am reading the TOC from a SQL Server database.
I describe below my approach to this problem. Here is what I am asking: Is
this a good way to load an arbitrary TOC from a database into an object
model? Is there a better way to approach the problem?
The TreeView can be bound to a hierarchical object model. So the problem
becomes how to load the data into a hierarchical object model. So, we start
with the database, which has a table called TOC. Each node in the TOC object
model has a corresponding record in the table. The table has four columns:
ID, Caption, Parent, and Level.
-- The ID column contains the ID for the node.
-- The Caption column contains the caption displayed for the node.
-- The Parent column contains the ID of the node's parent node.
-- The Level column contains the level at which the node can be found.
Top-level nodes have a Level of 0, nodes 1 level down have a Level of 1, and
so on. We sort the table on this column to ensure that higher-level nodes
get loaded first.
The table is initialized with a root-level node record, to which we assign a
Level of -1. All nodes with a Level of 0 have a Parent value of -1, which
means the root node is their parent.
A TocNode object has properties for ID, Caption, Parent, and Level. It also
has a Nodes collection for its child nodes. The root-level record (Level -1)
is loaded into a root-level TocNode object, then the Level 0 nodes are
loaded into its Nodes collection. The level 1 nodes are loaded into the
Nodes collections of their level 0 parents, and so on.
To make it easier to get to parent nodes while loading, each node is also
loaded into a temporary Dictionary that is keyed to the node's ID. When a
later node is loaded, the app reads that node's Parent property and
retrieves the parent node from the Dictionary. That way, the app doesn't
have to search the tree for a parent as each node is loaded. The Dictionary
is discarded when the load is complete, and the root node is bound to the
TreeView control.
That's it in a nutshell. To keep things simple, I've left out quite a bit,
such as article ID references and so on. So, is this a good way to go, or is
there a simpler way to get the job done? Thanks in advance for your help.
David Veeneman
Foresight Systems
Here is my scenario: I have a TreeView that represents a Table of Contents.
The TOC has an arbitrary number of branches, and each branch has an
arbitrary depth. I am reading the TOC from a SQL Server database.
I describe below my approach to this problem. Here is what I am asking: Is
this a good way to load an arbitrary TOC from a database into an object
model? Is there a better way to approach the problem?
The TreeView can be bound to a hierarchical object model. So the problem
becomes how to load the data into a hierarchical object model. So, we start
with the database, which has a table called TOC. Each node in the TOC object
model has a corresponding record in the table. The table has four columns:
ID, Caption, Parent, and Level.
-- The ID column contains the ID for the node.
-- The Caption column contains the caption displayed for the node.
-- The Parent column contains the ID of the node's parent node.
-- The Level column contains the level at which the node can be found.
Top-level nodes have a Level of 0, nodes 1 level down have a Level of 1, and
so on. We sort the table on this column to ensure that higher-level nodes
get loaded first.
The table is initialized with a root-level node record, to which we assign a
Level of -1. All nodes with a Level of 0 have a Parent value of -1, which
means the root node is their parent.
A TocNode object has properties for ID, Caption, Parent, and Level. It also
has a Nodes collection for its child nodes. The root-level record (Level -1)
is loaded into a root-level TocNode object, then the Level 0 nodes are
loaded into its Nodes collection. The level 1 nodes are loaded into the
Nodes collections of their level 0 parents, and so on.
To make it easier to get to parent nodes while loading, each node is also
loaded into a temporary Dictionary that is keyed to the node's ID. When a
later node is loaded, the app reads that node's Parent property and
retrieves the parent node from the Dictionary. That way, the app doesn't
have to search the tree for a parent as each node is loaded. The Dictionary
is discarded when the load is complete, and the root node is bound to the
TreeView control.
That's it in a nutshell. To keep things simple, I've left out quite a bit,
such as article ID references and so on. So, is this a good way to go, or is
there a simpler way to get the job done? Thanks in advance for your help.
David Veeneman
Foresight Systems