Dynamic TreeView from DB / Database - best logic, VB.net Windows Form
Posted on 2004-10-26
I have an application that requires the use of a TreeView control. I am able to apply the MS TreeView to my windows form and can get it to do the basic stuff. However, my users need to be able to add new nodes via updating an SQL database (not interactively - via a Stored Procedure, or manual updating), and the new root/parent/child/grandchild ends up being the last record in the table. I run an SQL statement to sort everything by ParentID, Level and Sequence. Still, I am having a difficult time in creating the most easiest to use structure, and I am finding that I am having to jump through hoops in my programming logic to get it to work. I strongly feel that there must be an easier way, but I'm just not able to see it (I think I might be stuck on my last project that did all of this via VB 6 and user controls - that compared to this seemed easy). I would greatly appreciate any advice on how I might explore other alternatives to the path I am following.
iID, sName, iParentID, iLevel, iSequence
iID sNAME iPARENTID iLEVEL iSEQUENCE
1 Batch Report Group 0 0 1
2 Machine Report Group 0 0 2
3 Management Reports 0 0 3
4 Performance Reports 0 0 4
5 Misc Reports 0 0 5
6 Find 0 0 6
7 Batch List Pr 1 1 1
8 Batch Status Pr 1 1 4
9 Machine Batch Pr 2 1 1
10 Mach Maintenance Pr 2 1 4
12 BatchSize Summary 3 1 1
To build the tree in VB.Net:
This is where I get stuck - and need a breath of fresh air.
- I cannot find a way to sort the table in a top-down structure (Root/Parent/etc.) without the user having to manually redo the sort (a new - and unwanted process/field) each time something new is added to the table.
- I find that I can not do this in a single step programmaticaly
- For my first pass through I am using SQLDataReader - and can add new levels to the root
- I then call another query to obtain the children - but can't get past this point - with the structure I have.
It's frustrating because if I had a table that I could just read through this would be easy - but that's not going to happen. Need a new perspective here - I appreciate your help and experience. If you would llike to see my code attempts (some of my commented out code came close - but didn't do the the job), I'll include it. But I am thinking I really do need a fresh approach.