Solved

Tree structure from table

Posted on 2003-11-25
10
2,855 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
10 Comments
 
LVL 2

Author Comment

by:gsteup
Comment Utility
"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
Comment Utility
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
Comment Utility
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
 
LVL 5

Expert Comment

by:volking
Comment Utility
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 250 total points
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 18

Expert Comment

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

Expert Comment

by:volking
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now