Tree structure from table

I want to implement data that is structured in a tree.

Now I know of several ways of creating a table that could represent a table. My problem is how to get that data out of the table into a control (the data binding process).

I need to be able to show the whole tree, collapse branches, select branches, drag'n'drop branches... Oh yes and it is not like a treeview but more like a treelist, that will show all data columns of the table.

I would like to implement this through a ASP.NET page. Can use C# or VB.NET but other platforms are welcome too.

Not looking for a detailed solution (although I will accept it :) ) but some hints in the right direction would be great.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gsteupAuthor Commented:
"Now I know of several ways of creating a table that could represent a table". I ment "could represent a TREE" of course.
And I forgot to mention that I need to be able to change, insert and delete data rows in, into and from the tree.
What you need to have probably is a grid control embedded in the tree structure and write code behind the cell changes or bind the control to the grid.
download MS IEWebControls.exe,
Install it,
fix couple bugs related to tree node expanding and persistance support on the server side by overloading of its classes TreeView and TreeNode, these bugs are not too complex, but I don't have code nigh at hand,
create basic aspx file that selects top level nodes and fullfills them with data:
                DataReader r;
                TreeNode item;
                doIt = r.Read();
                  item = new TreeNode();

                  item.NodeData =  Convert.ToString(r.GetString(0));
                  item.Text =  r.GetString(1);

                  item.TreeNodeSrc = "/next_level.aspx?level_id="+Convert.ToString(r.GetString(0));
                  item.ImageUrl = "/images/root_node.gif";
                  item.SelectedImageUrl = "/images/active_root_node.gif";
                  doIt = r.Read();


next_level.aspx will return on Page_Load() XML content of the specified level

                doIt = r.Read();
                    this.Response.Output.Write("<treenode NodeData=\"{0}\" Text=\"{1}\" ImageUrl=\"{2}\" SelectedImageUrl=\"{3}\" TreeNodeSrc=\"{4}\" />",
                    doIt = r.Read();


Also, you could look for another ASPX Tree controls in Web.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Here's a similar problem and answer ... it too may help your effort.

One more point ... I too was using the MS IEWebControls.exe mentioned earlier when I finally got the code in the above post working. After a lot of useless effort, I eventually decided the tree control in MS IEWebControls.exe did not have the flexability and robustness I needed. (Primarily because, like you, I needed a "simple" way to add and delete nodes on-the-fly)

Eventually I ended up purchasing the Infragistics NetAdvantage products. I now use their WinTree and WebNavigator tree in my applications and I've been very pleased.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
This will take a hierarchy and return it in a resultset
General way to handle trees. Fails when the sequence varchar length is exceeded - in this case 100 levels.
Author Nigel Rivett

-- create test data
create table #z (id int, name varchar(20), Parent int null)
insert #z select   1,  'foo', null
insert #z select     2,  'foo2',   null
insert #z select     3,  'foo2a',      2
insert #z select    11,  'foo2aa',     3
insert #z select    12,  'foo2aaa',   11
insert #z select    13,  'foo2ab',     3
insert #z select    14,  'foo2ac',     3
insert #z select     4,  'foo2b',      2
insert #z select     5,  'foo3',   null
insert #z select     6,  'foo3b',      5
insert #z select     7,  'foo3ba',     6
insert #z select     8,  'foo4',   null
insert #z select     9,  'fooa',       1
insert #z select    10,  'foo2ba',     4

-- Get the hierarchy
create table #tree (id int, sequence varchar(1000), levelNo int)
-- insert top level (to get sub tree just insert relevent id here)
insert #tree select id, right(space(10) + convert(varchar(10),id),10), 1 from #z where Parent is null
declare @i int
select @i = 0
-- keep going until no more rows added
while @@rowcount > 0
     select @i = @i + 1
     insert #tree
     -- Get all children of previous level
     select, sequence + right(space(10) + convert(varchar(10),,10), @i + 1
     from #z, #tree
     where #tree.levelNo = @i
     and #z.Parent =

-- output with hierarchy formatted
select space((levelNo-1)*4) +
from #tree, #z
where =
order by sequence

drop table #tree
drop table #z


gsteupAuthor Commented:
Alright, thanks for all the input so far.
The solution that comes closest to what I need, was posted by volking


I looked at the solution from Infragistics NetAdvantage you recommened (downloaded the trial version). I have not done very much with it but looked at some of their help files and examples. All hirachical grids are based on relations between different tables.
Have you implemented a hirachical tree with their DataGrid? I was wondering, how I would have to bind the individual layers of the tree to each other?
Yes, I've used the hierarchial metaphor with the datagrid ... simply put .... the learning curve was VERY PAINFUL (at least it was for me). The hierarchial metaphor's primary characteristic is STRONGLY TYPED DATASETS. When I began using the Infragistic datagrid, I knew nothing about Strongly Typed Datasets. However, once mastered, it is an extremely powerful solution. As I remember, there's at least one tutorial which walks through the process. Do the tutorial concentrating on each step and figure out what's "really" happening with special focus on the features of Strongly Typed Datasets and the way a relationship can be established between datasets.

In the end, I found the learning curve worth the effort. Good luck ...

gsteupAuthor Commented:
I have finallized my implementation:

My table has the following fields:
id int
name char[100]
parent int
idx int

where parent indicates the parent node and idx indicates the position within the local branch. A reference to first child and next sibling are not necessary for me at this moment. (The real table has actually way more data columns)

I read each level of the tree beginning from the top layer (parent = 0) and then using the returned data to filter all the children of the next one. (SELECT * FROM level2 WHERE (level2.parent IN ORDER BY (level2.parent,idx))
All results are inserted as a table into a DataSet and linked through relations.

From there on it is just a question of which control to use. Since no hierarchical grid is included in the standard MS controls either I will have to write one myself or I just go with an already available one (like Infragistics as recommended by volking)

Strongly typed DataSet are very useful for this implementation but they are not necessary. I managed to bind to the data hierarchically without it and it worked fine, but of course what not as flexible as I wanted it to be. Thanks for all the help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.