Click here to Skip to main content
15,886,530 members
Articles / Web Development / ASP.NET
Article

TB.TreeGrid

Rate me:
Please Sign up or sign in to vote.
4.76/5 (19 votes)
28 Sep 2008LGPL3 562.1K   2.1K   58   71
An ASP.NET Webcontrol used to show hierarchy data in grid view

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.

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

C#
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

  • 29th September, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Collapse Pin
sherwinzhu3-Aug-09 1:25
sherwinzhu3-Aug-09 1:25 
GeneralRe: Collapse Pin
diego.palumbo3-Aug-09 21:13
diego.palumbo3-Aug-09 21:13 
GeneralStyle for the header row and sum the values of child node columns Pin
manasbeckham26-Jul-09 18:38
manasbeckham26-Jul-09 18:38 
GeneralRe: Style for the header row and sum the values of child node columns Pin
sherwinzhu3-Aug-09 1:22
sherwinzhu3-Aug-09 1:22 
Questionone error that in dotnet framework 2.0 ? Pin
Todd Zhao23-Jul-09 22:44
Todd Zhao23-Jul-09 22:44 
AnswerRe: one error that in dotnet framework 2.0 ? Pin
Todd Zhao23-Jul-09 22:50
Todd Zhao23-Jul-09 22:50 
GeneralRe: one error that in dotnet framework 2.0 ? Pin
sherwinzhu23-Jul-09 22:53
sherwinzhu23-Jul-09 22:53 
AnswerRe: one error that in dotnet framework 2.0 ? Pin
sherwinzhu23-Jul-09 22:52
sherwinzhu23-Jul-09 22:52 
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 Pin
Watson Jason20-Jul-09 15:50
Watson Jason20-Jul-09 15:50 
GeneralRe: you are great Pin
sherwinzhu23-Jul-09 22:53
sherwinzhu23-Jul-09 22:53 
QuestionCan you give another version of this control which can run under dotnet framework 2.0 ???? Pin
pcperson11-May-09 10:26
pcperson11-May-09 10:26 
AnswerRe: Can you give another version of this control which can run under dotnet framework 2.0 ???? Pin
sherwinzhu13-May-09 9:05
sherwinzhu13-May-09 9:05 
QuestionHow does it support DragDrop feature? Pin
Jian123456731-Dec-08 6:17
Jian123456731-Dec-08 6:17 
GeneralDataItem and viewstate Pin
ashorrock@hotmail.com28-Nov-08 1:28
ashorrock@hotmail.com28-Nov-08 1:28 
GeneralRe: DataItem and viewstate Pin
sherwinzhu28-Nov-08 5:14
sherwinzhu28-Nov-08 5:14 
Generalhelp me "e.Row.Cells(4).Attributes.Add" Pin
eagleforce23-Nov-08 15:14
eagleforce23-Nov-08 15:14 
GeneralRe: help me "e.Row.Cells(4).Attributes.Add" Pin
sherwinzhu26-Nov-08 4:27
sherwinzhu26-Nov-08 4:27 
Generalthis is my code Pin
eagleforce23-Nov-08 4:38
eagleforce23-Nov-08 4:38 
GeneralRe: this is my code Pin
sherwinzhu23-Nov-08 4:44
sherwinzhu23-Nov-08 4:44 
GeneralRe: this is my code Pin
eagleforce23-Nov-08 4:50
eagleforce23-Nov-08 4:50 
GeneralRe: this is my code Pin
sherwinzhu23-Nov-08 4:47
sherwinzhu23-Nov-08 4:47 
GeneralSelectedRowStyle BackColor="Red" Pin
eagleforce23-Nov-08 4:13
eagleforce23-Nov-08 4:13 
GeneralRe: SelectedRowStyle BackColor="Red" Pin
sherwinzhu23-Nov-08 4:25
sherwinzhu23-Nov-08 4:25 
Generalcomplete function like this Pin
eagleforce23-Nov-08 4:05
eagleforce23-Nov-08 4:05 
GeneralRe: complete function like this Pin
sherwinzhu23-Nov-08 4:26
sherwinzhu23-Nov-08 4:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.