?
Solved

Tree structure from table

Posted on 2003-11-25
10
Medium Priority
?
2,871 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:gsteup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 2

Author Comment

by:gsteup
ID: 9817282
"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.
0
 
LVL 1

Expert Comment

by:ms_praveen
ID: 9817301
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.
0
 
LVL 15

Expert Comment

by:OMC2000
ID: 9818325
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.
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 5

Expert Comment

by:volking
ID: 9823807
Here's a similar problem and answer ... it too may help your effort.

http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_20775087.html

enjoy
Frederick
0
 
LVL 5

Accepted Solution

by:
volking earned 1000 total points
ID: 9823850
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.

Frederick
0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 9844402
This will take a hierarchy and return it in a resultset
http://www.nigelrivett.net/RetrieveTreeHierarchy.html
0
 
LVL 5

Expert Comment

by:volking
ID: 9849573
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
*/


0
 
LVL 2

Author Comment

by:gsteup
ID: 9854252
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?
0
 
LVL 5

Expert Comment

by:volking
ID: 9857692
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
0
 
LVL 2

Author Comment

by:gsteup
ID: 9866126
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
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question