Link to home
Start Free TrialLog in
Avatar of gsteup
gsteup

asked on

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.
Avatar of gsteup
gsteup

ASKER

"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.
Avatar of OMC2000
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();
                while(doIt)
                {
                  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";
                  tree.Nodes.Add(item);
                  doIt = r.Read();

                }

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

                this.Response.Output.WriteLine("<TREENODES>");
                doIt = r.Read();
                while(doIt)
                {
                    this.Response.Output.Write("<treenode NodeData=\"{0}\" Text=\"{1}\" ImageUrl=\"{2}\" SelectedImageUrl=\"{3}\" TreeNodeSrc=\"{4}\" />",
                        Convert.ToString(r.GetDecimal(1)),r.GetString(0),
                        "/images/nodeoff.GIF",
                        "/images/nodeon.GIF",
                        "next_level.aspx?level_id="+Convert.ToString(r.GetDecimal(1)));
                    doIt = r.Read();
                }
                this.Response.Output.WriteLine("</TREENODES>");

enjoy;)

Also, you could look for another ASPX Tree controls in Web.
Here's a similar problem and answer ... it too may help your effort.

https://www.experts-exchange.com/questions/20775087/Recursive-Select.html

enjoy
Frederick
ASKER CERTIFIED SOLUTION
Avatar of volking
volking

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This will take a hierarchy and return it in a resultset
http://www.nigelrivett.net/RetrieveTreeHierarchy.html
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
begin
     select @i = @i + 1
     insert #tree
     -- Get all children of previous level
     select #z.id, sequence + right(space(10) + convert(varchar(10),#z.id),10), @i + 1
     from #z, #tree
     where #tree.levelNo = @i
     and #z.Parent = #tree.id
end

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

drop table #tree
drop table #z

/*  OUTPUT
foo
    fooa
foo2
    foo2a
        foo2aa
            foo2aaa
        foo2ab
        foo2ac
    foo2b
        foo2ba
foo3
    foo3b
        foo3ba
foo4
*/


Avatar of gsteup

ASKER

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

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 ...

Volking
Avatar of gsteup

ASKER

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 level1.id) 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)

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