hierarchical implementation question

  • Thread starter Thread starter jayson_13
  • Start date Start date
J

jayson_13

Hi,

I want to retrieve the hierarchical structure which is store inside
the database and then display it using treeview control. But now I
got a design issue which I hope that you guys can help me. My
hierarchical structure has five levels which is store in table call
level1, level2,… level5. Each level remember the parent ID e.g. level5
contains a column called level4ID. How should I implement it in order
to get fastest loading?
Currently what I think of is that fill the records in level1,
level2, level3, level4, level5 into the same dataset. Then use nested
loop to implement the treeview.
Can anyone give me more efficient solution? Seems like I am a stupid
programmer.

foreach(DataRow dr in dsAcctChart.Tables[Level1].Rows)
{
treeview1.Nodes.Add(dr["Name"].ToString());
DataRow [] drows=dsAcctChart.Tables[Level2].Select("Level1ID=" +
dr["ID"].ToString());
foreach(DataRow dr1 in drows)
{
if(treeView1.SelectedNode == null)
treeView1.SelectedNode=treeView1.Nodes[Level1NodeCount];
treeView1.SelectedNode.Nodes.Add(dr1["Name"].ToString());
foreach(..Level3)
….
foreach(….Level4)
…..
foreach(… Level5)
}
}
 
jayson_13 said:
Hi,

I want to retrieve the hierarchical structure which is store inside
the database and then display it using treeview control. But now I
got a design issue which I hope that you guys can help me. My
hierarchical structure has five levels which is store in table call
level1, level2,. level5. Each level remember the parent ID e.g. level5
contains a column called level4ID. How should I implement it in order
to get fastest loading?
Currently what I think of is that fill the records in level1,
level2, level3, level4, level5 into the same dataset. Then use nested
loop to implement the treeview.
Can anyone give me more efficient solution? Seems like I am a stupid
programmer.

Loading data-heavy tree-views can be tricky and time-consuming. There's two
things I do which may apply to your situation.

1. Lazy-Loading - on form load, load only the top level treeview nodes.
Then, hook the expand event so that whan the user expands a node, you
quickly load its child nodes from the database and insert them into the
tree. I usually pin data to the Tag of each TreeNode which tells me what
information needs to be loaded for the children of that node. SqlBuddy
(http://sqlbuddy.sourceforge.net) uses this approach to load in the Database
Explorer - and it's pretty darn quick! You can see the idea in the link
below( watch for breaks) - look for the HandleExplandNode function about 1/4
down the page:

http://cvs.sourceforge.net/viewcvs....rev=1.73&content-type=text/vnd.viewcvs-markup

2. Freezing the View - by visually freezing the view and showing an
hourglass, you can at least reduce the impression that loading of the tree
is slow. I think a flickering tree makes the user very aware that things are
taking time! In buddy I used a WinAPI call to freeze the tree as it is
cleared and reloaded. Again, you can see this in the first two lines of the
HandleExpandNode method.

Hope this helps

Tobin Harris
 
Back
Top