Click here to Skip to main content
6,594,932 members and growing! (15,032 online)
Email Password   helpLost your password?
Web Development » Custom Controls » General     Advanced License: The GNU Lesser General Public License

TB.TreeGrid

By sherwinzhu

An ASP.NET Webcontrol used to show hierarchy data in grid view
C# (C# 2.0), .NET (.NET 2.0, .NET 3.0, .NET 3.5), ASP.NET, Dev
Posted:28 Sep 2008
Views:22,812
Bookmarked:41 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
16 votes for this article.
Popularity: 5.18 Rating: 4.30 out of 5
1 vote, 6.3%
1
1 vote, 6.3%
2

3
3 votes, 18.8%
4
11 votes, 68.8%
5

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

  • 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

About the Author

sherwinzhu


Member

Location: China China

Other popular Custom Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 54 (Total in Forum: 54) (Refresh)FirstPrevNext
GeneralTreeGridDemo.dll Pinmemberdiego.palumbo0:19 25 Sep '09  
GeneralDoesn't work with Chrome or Firefox Pinmembermanasbeckham18:19 7 Sep '09  
GeneralRe: Doesn't work with Chrome or Firefox Pinmemberdiego.palumbo0:14 25 Sep '09  
GeneralContext menu... Pinmemberdiego.palumbo22:18 3 Aug '09  
GeneralAccess all the nodes Pinmembermanasbeckham4:17 3 Aug '09  
GeneralRe: Access all the nodes Pinmembersherwinzhu22:30 3 Aug '09  
GeneralRe: Access all the nodes Pinmembermanasbeckham22:02 4 Aug '09  
Generaldocumentation Pinmemberdiego.palumbo1:11 3 Aug '09  
GeneralRe: documentation Pinmembersherwinzhu2:28 3 Aug '09  
GeneralRe: documentation Pinmemberdiego.palumbo22:08 3 Aug '09  
GeneralCollapse Pinmemberdiego.palumbo0:41 3 Aug '09  
GeneralRe: Collapse Pinmembersherwinzhu2:25 3 Aug '09  
GeneralRe: Collapse Pinmemberdiego.palumbo22:13 3 Aug '09  
GeneralStyle for the header row and sum the values of child node columns Pinmembermanasbeckham19:38 26 Jul '09  
GeneralRe: Style for the header row and sum the values of child node columns Pinmembersherwinzhu2:22 3 Aug '09  
Answerone error that in dotnet framework 2.0 ? PinmemberTodd Zhao23:44 23 Jul '09  
GeneralRe: one error that in dotnet framework 2.0 ? PinmemberTodd Zhao23:50 23 Jul '09  
GeneralRe: one error that in dotnet framework 2.0 ? Pinmembersherwinzhu23:53 23 Jul '09  
GeneralRe: one error that in dotnet framework 2.0 ? Pinmembersherwinzhu23:52 23 Jul '09  
Generalyou are great Pinmemberdefineconst16:50 20 Jul '09  
GeneralRe: you are great Pinmembersherwinzhu23:53 23 Jul '09  
GeneralCan you give another version of this control which can run under dotnet framework 2.0 ???? Pinmemberpcperson11:26 11 May '09  
GeneralRe: Can you give another version of this control which can run under dotnet framework 2.0 ???? Pinmembersherwinzhu10:05 13 May '09  
GeneralHow does it support DragDrop feature? PinmemberJian12345677:17 31 Dec '08  
GeneralDataItem and viewstate Pinmemberashorrock@hotmail.com2:28 28 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Sep 2008
Editor: Deeksha Shenoy
Copyright 2008 by sherwinzhu
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project