Click here to Skip to main content
6,306,412 members and growing! (16,589 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:16,959
Bookmarked:34 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
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 33 (Total in Forum: 33) (Refresh)FirstPrevNext
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  
GeneralRe: DataItem and viewstate Pinmembersherwinzhu6:14 28 Nov '08  
Generalhelp me "e.Row.Cells(4).Attributes.Add" Pinmembereagleforce16:14 23 Nov '08  
GeneralRe: help me "e.Row.Cells(4).Attributes.Add" Pinmembersherwinzhu5:27 26 Nov '08  
Generalthis is my code Pinmembereagleforce5:38 23 Nov '08  
GeneralRe: this is my code Pinmembersherwinzhu5:44 23 Nov '08  
GeneralRe: this is my code Pinmembereagleforce5:50 23 Nov '08  
GeneralRe: this is my code Pinmembersherwinzhu5:47 23 Nov '08  
GeneralSelectedRowStyle BackColor="Red" Pinmembereagleforce5:13 23 Nov '08  
GeneralRe: SelectedRowStyle BackColor="Red" Pinmembersherwinzhu5:25 23 Nov '08  
Generalcomplete function like this Pinmembereagleforce5:05 23 Nov '08  
GeneralRe: complete function like this Pinmembersherwinzhu5:26 23 Nov '08  
GeneralRe: complete function like this Pinmembersherwinzhu5:35 23 Nov '08  
Generalwhy not display the color Pinmembereagleforce4:02 23 Nov '08  
GeneralRe: why not display the color Pinmembersherwinzhu4:47 23 Nov '08  
Generalhow to add "onmouseover" for row's Attributes Pinmembereagleforce20:07 21 Nov '08  
GeneralRe: how to add "onmouseover" for row's Attributes Pinmembersherwinzhu17:13 22 Nov '08  
GeneralRe: how to add "onmouseover" for row's Attributes Pinmembereagleforce2:13 23 Nov '08  
QuestionCan you help me PinmemberILHT17:40 21 Nov '08  
AnswerRe: Can you help me Pinmembersherwinzhu17:03 22 Nov '08  
GeneralRe: Can you help me PinmemberILHT16:58 23 Nov '08  
GeneralRe: Can you help me Pinmembersherwinzhu7:30 26 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
Web13 | Advertise on the Code Project