Click here to Skip to main content
15,880,725 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 561.9K   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

 
QuestionUnable to set property 'display' of undefined or null reference Pin
lalitjoshilalit10-Sep-14 0:33
lalitjoshilalit10-Sep-14 0:33 
Questionunable to expand past 2nd level node Pin
Robert Glassett20-May-13 11:15
Robert Glassett20-May-13 11:15 
Questiontr.style is undefined in firebug and also cant collapse a node in firefox Pin
deni_syahreza30-Mar-13 0:27
deni_syahreza30-Mar-13 0:27 
GeneralMy vote of 5 Pin
PSK_20-Jun-12 22:23
PSK_20-Jun-12 22:23 
Nice
QuestionHow pagging in tb.treeview? Pin
suco_20047-Apr-11 18:06
suco_20047-Apr-11 18:06 
QuestionNodeCell issue [modified] Pin
irene kuan12-Jan-11 16:46
irene kuan12-Jan-11 16:46 
Generaltr.style is undefined on firefox 3.5.13 when collapsing the node. Pin
yousaf2k5-Oct-10 0:36
yousaf2k5-Oct-10 0:36 
GeneralRe: tr.style is undefined on firefox 3.5.13 when collapsing the node. Pin
lalitjoshilalit10-Sep-14 0:40
lalitjoshilalit10-Sep-14 0:40 
GeneralComplete Source Code of The Control Pin
yousaf2k30-Aug-10 21:31
yousaf2k30-Aug-10 21:31 
GeneralAccess all the nodes Pin
JyothiPrajna6-Jul-10 16:28
JyothiPrajna6-Jul-10 16:28 
GeneralRe: Access all the nodes Pin
tylerjaques11-Nov-10 7:28
tylerjaques11-Nov-10 7:28 
GeneralScrolling in TreeGrid Pin
JyothiPrajna30-Jun-10 17:03
JyothiPrajna30-Jun-10 17:03 
GeneralDatabind from a datasource for this control.. Please help Pin
JyothiPrajna29-Jun-10 7:23
JyothiPrajna29-Jun-10 7:23 
Generalit doesn`t work with firefox Pin
Eslam Zien8-Dec-09 3:16
Eslam Zien8-Dec-09 3:16 
Generaldll not working in shared hosting Pin
Ranjanahere25-Nov-09 21:48
Ranjanahere25-Nov-09 21:48 
GeneralTreeGridDemo.dll Pin
diego.palumbo24-Sep-09 23:19
diego.palumbo24-Sep-09 23:19 
GeneralDoesn't work with Chrome or Firefox Pin
manasbeckham7-Sep-09 17:19
manasbeckham7-Sep-09 17:19 
GeneralRe: Doesn't work with Chrome or Firefox Pin
diego.palumbo24-Sep-09 23:14
diego.palumbo24-Sep-09 23:14 
GeneralContext menu... Pin
diego.palumbo3-Aug-09 21:18
diego.palumbo3-Aug-09 21:18 
GeneralAccess all the nodes Pin
manasbeckham3-Aug-09 3:17
manasbeckham3-Aug-09 3:17 
GeneralRe: Access all the nodes Pin
sherwinzhu3-Aug-09 21:30
sherwinzhu3-Aug-09 21:30 
GeneralRe: Access all the nodes Pin
manasbeckham4-Aug-09 21:02
manasbeckham4-Aug-09 21:02 
GeneralRe: Access all the nodes Pin
JyothiPrajna6-Jul-10 16:25
JyothiPrajna6-Jul-10 16:25 
GeneralRe: Access all the nodes [Download Complete Source from here] Pin
yousaf2k30-Aug-10 21:29
yousaf2k30-Aug-10 21:29 
Generaldocumentation Pin
diego.palumbo3-Aug-09 0:11
diego.palumbo3-Aug-09 0:11 

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.