Click here to Skip to main content
Email Password   helpLost your password?

Introduction

The TB.TreeGrid is an ASP.NET Webcontrol used to show hierarchy data in grid view.

Usually, we use GridView Webcontrol to show hierarchy data, but it has only one column. It is not suitable for BOM or schedule plan, etc.

This control combines the advantages of GridView and TreeView, can show hierarchy data in multiple columns. It supports databind, postback, callback, Microsoft Ajax, Event (Edit, Select, Expand, Populate, Collapse, Remove, Update, Cancel, etc).

Please go here to download the latest source and demo.

TreeGrid.png

Using the Code

It has similar syntax to TreeView and GridView.

First, add TB.Web.UI.TreeGrid.dll as reference, you can find it in the bin directory.

Second, create a *.aspx file, add the control to the page.

 <tbwc:treegrid ID="TreeGrid" runat="server" ShowHeader="true" 
    ShowFooter ="false" Caption="This is TreeGird's Caption" 
    ExpandDepth="1" PopulateNodesFromClient="false"
    CaptionAlign="Left" NodeWrap="false" LineImagesFolder="~/TreeViewLines" 
    ShowLines="true" CssClass="tgDefault" ImageSet="XPFileExplorer" 
    NodeCellPosition="1" OnSelectedNodeChanged="TreeGrid_SelectedNodeChanged" 
    OnNodePopulate="TreeGrid_NodePopulate" OnRowCommand="TreeGrid_RowCommand" 
    onNodeDataBound="TreeGrid_NodeDataBound" >
<HeaderStyle BackColor="Gainsboro" />
<NodeStyle Display="Inline" />
<Columns>
    <asp:TemplateField HeaderText="FirstColumn">
        <itemtemplate><%# Eval("C1") %></itemtemplate>
        <ItemStyle Width="100px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="???">
        <itemtemplate><%# Eval("C2") %></itemtemplate>
        <ItemStyle Width="200px" />
    </asp:TemplateField>
        <asp:TemplateField HeaderText="???">
        <itemtemplate><%# Eval("C3")  %></itemtemplate>
        <ItemStyle Width="100px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="???">
        <itemtemplate><asp:Literal ID="LtrForth" Text='<%# Eval("C4")  %>' 
    runat="server"></asp:Literal></itemtemplate>
        <ItemStyle Width="100px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="???">
        <itemtemplate><%# Eval("C5") %></itemtemplate>
        <ItemStyle Width="60px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="???">
        <itemtemplate><%# Eval("C6") %></itemtemplate>
        <edititemtemplate><asp:TextBox ID="TxtSixth" Text='<%# Eval("C6") %>' 
    runat="server" Width="60px" Height="12px">
    </asp:TextBox></edititemtemplate>
        <ItemStyle Width="100px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="???">
    <itemstyle cssclass="listOp" />
    <itemtemplate>
        <asp:ImageButton ID="LbtnEdit" runat="server" CommandName="Edit" 
            ToolTip="??" ImageUrl="img/opEdit.gif"></asp:ImageButton>
        <asp:LinkButton ID="LbtnUpdate" runat="server" CommandName="Update" 
            Text="Update"></asp:LinkButton>
        <asp:LinkButton ID="LbtnCancle" runat="server" CommandName="Cancel" 
            Text="Cancel"></asp:LinkButton>
        <asp:ImageButton ID="LbtnDel" runat="server" CommandName="Remove" 
            ToolTip="??" ImageUrl="img/opDel.gif"></asp:ImageButton>
    </itemtemplate>
</asp:TemplateField>
</Columns>
</tbwc:treegrid>

Third, open *.cs file, write databind code and event handler.

protected void Page_Load(object sender, EventArgs e)
{
	if (!Page.IsPostBack)
	{
		Builder.InitTreeGridManual(this.TreeGrid);
	}
}

protected void TreeGrid_SelectedNodeChanged
	(object sender, TB.Web.UI.WebControls.TreeGridNodeEventArgs e)
{
}

protected void TreeGrid_RowCommand
	(object sender, TB.Web.UI.WebControls.TreeGridRowCommandEventArgs e)
{
	this.LblHint.Text = "?????[Event Name:" +  e.CommandName+ "] " + 
	DateTime.Now.ToString();
	if (e.CommandName == "Edit")
	{
		// previous EditingNode
		TreeGridNode oNode = this.TreeGrid.EditingNode; 

		// set current EditingNode
		e.Row.Owner.Edit();                             
	
		// cancel previous EditingNode
		if (oNode != null)                            
		{
			oNode.DataItem = Builder.GetNodeData(oNode.Value);
			oNode.RaiseNodeDataBind(false);
		}
		// bind data
		e.Row.Owner.DataItem = Builder.GetNodeData(e.Row.Owner.Value);  

		e.Row.Owner.RaiseNodeDataBind(false);
	}
	if (e.CommandName == "Update")
	{
		e.Row.Owner.Editing = false;
		string newValue = ((TextBox)e.Row.FindControl("TxtSixth")).Text;
		// save .....
		e.Row.Owner.DataItem = Builder.GetNodeData(e.Row.Owner.Value);
		((Foo)e.Row.Owner.DataItem).C6 = newValue;      // for demo
		e.Row.Owner.RaiseNodeDataBind(false);
	}
	if (e.CommandName == "Cancel")
	{
		e.Row.Owner.Editing = false;
		e.Row.Owner.DataItem = Builder.GetNodeData(e.Row.Owner.Value);
		e.Row.Owner.RaiseNodeDataBind(false);
	}
	if (e.CommandName == "Remove")
	{
		if (e.Row.Owner.Parent != null)
		{
			e.Row.Owner.Parent.ChildNodes.Remove(e.Row.Owner);
		}
		else
		{
			this.TreeGrid.Nodes.Remove(e.Row.Owner);
		}
	}
}

protected void TreeGrid_NodePopulate
	(object sender, TB.Web.UI.WebControls.TreeGridNodeEventArgs e)
{
	TreeGridNode node = new TreeGridNode("new1");
	node.DataItem = Builder.GetNodeData("-1");
	e.Node.ChildNodes.Add(node);
	node.PopulateOnDemand = true;
	node.RaiseNodeDataBind(false);
	node = new TreeGridNode("new2");
	node.DataItem = Builder.GetNodeData("-1");
	e.Node.ChildNodes.Add(node);
	node.PopulateOnDemand = true;
	node.RaiseNodeDataBind(false);
}

protected void TreeGrid_NodeDataBound(object sender, TreeGridRowEventArgs e)
{
	if (e.Row.RowType == DataControlRowType.DataRow)
	{
		if (e.Row.RowState == DataControlRowState.Edit)
		{
		        ((LinkButton)e.Row.FindControl("LbtnUpdate")).Visible = true;
		        ((LinkButton)e.Row.FindControl("LbtnCancle")).Visible = true;
		}
		if (e.Row.RowState == DataControlRowState.Normal)
		{
		        ((LinkButton)e.Row.FindControl("LbtnUpdate")).Visible = false;
		        ((LinkButton)e.Row.FindControl("LbtnCancle")).Visible = false;
		}
	}
} 

Above is a simple introduction, I will update it shortly.

You can download the demo.zip and run it. The project is a Visual Studio 2008 project.

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalit doesn`t work with firefox
Eslam Zien
4:16 8 Dec '09  
hi,
first good job man
but it doesn`t work with firefox
Generaldll not working in shared hosting
Ranjanahere
22:48 25 Nov '09  
Hi , this is an excellent component. And we used it to build our web application to show schedule plan. But after uploading it to godaddy.com shared hosting server the dll is not working.I get error "System.Security.SecurityException: That assembly does not allow partially trusted callers". I searched on net and found that by adding assembly: AllowPartiallyTrustedCallers] to assemblyInfo.cs file and recompiling the dll it will work. Since godaddy shared hosting runs in trust level=medium. But since I dont have the code I cant recompile it. Can you please send TreeGrid.dll which can run trust level=medium. It would be a great help. We have already developed our application and really need this recompiled dll which can run in medium trust level. Thanks Ranjana.
GeneralTreeGridDemo.dll
diego.palumbo
0:19 25 Sep '09  
Could TreeGrid component work in a project without using TreeGridDemo.ddl?

Thanks

PS. Are going to make some new verision of this component? Do you need some help? I'm new to .NET world and I'm using your component for a university project...
GeneralDoesn't work with Chrome or Firefox
manasbeckham
18:19 7 Sep '09  
Hi,

Is there any specific reason why the Treegrid doesn't work with Google chrome or firefox?

Thanks and regards,
Manas
GeneralRe: Doesn't work with Chrome or Firefox
diego.palumbo
0:14 25 Sep '09  
I tried treeGrid with firefox, safari. chrome and it works fine...
GeneralContext menu...
diego.palumbo
22:18 3 Aug '09  
Hello sherwinzhu!
I have a new question for you: Do you think is possible to implement context menu function on each tree node?
If yes, how should I have to proceed in your opinion?
thank you
GeneralAccess all the nodes
manasbeckham
4:17 3 Aug '09  
Hi Sherwin,

I successfully used the NodeDataBound method for counting the SUM.
I am having trouble solving one more problem.

If I want to save all the changes with one button event instead of clicking each row button individually, how should I implement that.
In short, how can I traverse the entire tree grid, when it is databound. Can it be done by rows... or by nodes or any other way.

Thanks a lot.

Manas
GeneralRe: Access all the nodes
sherwinzhu
22:30 3 Aug '09  
Hi,
You can traverse the entire tree grid by nodes.
Every node has child nodes, and the node has a row property.
However, the row property of node is internal, it should change to public for access the row content.


sherwin
GeneralRe: Access all the nodes
manasbeckham
22:02 4 Aug '09  
Hi,

Thanks for the prompt answer but how do I change the property to public?

Regards,
Manas
Generaldocumentation
diego.palumbo
1:11 3 Aug '09  
Hi, where could I find the component documentation and example
Thanks
GeneralRe: documentation
sherwinzhu
2:28 3 Aug '09  
Sorry, I will update the source and documentation later after completing current project.
GeneralRe: documentation
diego.palumbo
22:08 3 Aug '09  
Thank you for your quick answer....
GeneralCollapse
diego.palumbo
0:41 3 Aug '09  
Hi! Thaks for your work!
How could I implement collapse function on each single node?
Thank you
GeneralRe: Collapse
sherwinzhu
2:25 3 Aug '09  
hi,
every node can collapse, when you click the node?
GeneralRe: Collapse
diego.palumbo
22:13 3 Aug '09  
in demo.zip downloaded collapse doesn't work...
I tried to add treegrid component attribute OnNodeCollapsed... and now collapse works
Thanks
GeneralStyle for the header row and sum the values of child node columns
manasbeckham
19:38 26 Jul '09  
Hi Sherwin,

Thanks a lot for this wonderful control.
I had two questions.
1. How do I make the parent node row appear in Bold?
2. How can I display the sum of the values of the child nodes/

Thanks and regards,
Manas
GeneralRe: Style for the header row and sum the values of child node columns
sherwinzhu
2:22 3 Aug '09  
Hi,
1, you can use NodeStyle attribute, like MS TreeView control, ParentNodeStyle, RootNodeStyle,LeafNodeStyle....

2, you can use a TemplateField in Columns, and in TreeGrid_NodeDataBound method, set the count of children.
Answerone error that in dotnet framework 2.0 ?
Todd Zhao
23:44 23 Jul '09  
first,i'm very sorry, my english is very poor . in the following sentence,it may be possible have some errors .
i have added the dll as reference. but i don't put the control in my page.my classmates said that 'it's necesary to install dotnet framework 3.5 '. in my project,i use dotframework 2.0 ,can you give me the vision of the control which can run under the dotnet framework 2.0?
GeneralRe: one error that in dotnet framework 2.0 ?
Todd Zhao
23:50 23 Jul '09  
dbillows@163.com ,this is my email.please give me an email,if you can.
GeneralRe: one error that in dotnet framework 2.0 ?
sherwinzhu
23:53 23 Jul '09  
sherwinzhu@126.com
GeneralRe: one error that in dotnet framework 2.0 ?
sherwinzhu
23:52 23 Jul '09  
The control can run in .net2.0, but the demo using 3.5. The reason is ajax.
May be the dll is build with .net3.5, you need download the source and rebuild with .net2.0.
Open the .sln with VS2008, set the target framework 2.0, add Reference System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, edit the web.config make use the setting of ajax is right.
All will be OK!
If you has problem, you can send email to me.
Generalyou are great
defineconst
16:50 20 Jul '09  
you are great
china boy
thank u very much

A Dream About Software。

GeneralRe: you are great
sherwinzhu
23:53 23 Jul '09  
Thanks
GeneralCan you give another version of this control which can run under dotnet framework 2.0 ????
pcperson
11:26 11 May '09  
Hi there,
I am trying to implement this solution using the dotnetframework 2.0...can you please provide me with the control & demo code for this version. Thanks so much
GeneralRe: Can you give another version of this control which can run under dotnet framework 2.0 ????
sherwinzhu
10:05 13 May '09  
rebuild the source with dotnetframework 2.0 and ms ajax.
or give me your email


Last Updated 29 Sep 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010