Click here to Skip to main content
15,892,517 members
Articles / Web Development / HTML

An ASP.NET DataGrid Custom Control to Freeze Header, Rows, Columns Just by Changing a Single Attribute Value

Rate me:
Please Sign up or sign in to vote.
4.64/5 (25 votes)
20 Feb 2006CPOL4 min read 286.6K   3.8K   91  
An ASP.NET DataGrid custom control which allows freezing of header, columns, and rows in a DataGrid just by changing a single attribute value. It is inherited from the .NET DataGrid, therefore all features of the DataGrid are still available.
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="FreezeHeader.WebForm1" %>
<%@ Register TagPrefix="Tittle" Namespace="Tittle.Controls" Assembly="Controls" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<title>WebForm1</title>
		<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
		<meta name="CODE_LANGUAGE" Content="C#">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
	</HEAD>
	<body MS_POSITIONING="GridLayout" onload="AddEmptyHeaderDataLoad()">
		<style>
		.greyed
		{
		  background-color:#c0c0c0;
		}
		</style>
		<script language=javascript>
		function AddEmptyHeaderDataLoad()
		{
			if ( typeof(dgTittle4) != 'undefined' )
			{
			   dgTittle4.rows[1].cells[0].innerText = 'Tittle Joseph';
			   dgTittle4.rows[1].cells[1].innerText = '29';
			   dgTittle4.rows[1].cells[2].innerText = '5/67 Rachna Vaishali Ghaziabad';
			}
		}		
		</script>		
		<form id="Form1" method="post" runat="server">
			&nbsp;
			<table>
				<tr>
					<td valign=top>
						<b><font color=red>Ex. I - Header Freeze</font></b><br>
						<Tittle:CustomDataGrid id="dgTittle" FreezeHeader=true
							runat="server" AutoGenerateColumns=False
							GridHeight="150"
							>
						<Columns>
							<asp:TemplateColumn HeaderText="User Name" >
								<ITEMTEMPLATE>
									<asp:Label Runat="server" ID="lblName" Width="100" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'  />
								</ITEMTEMPLATE>
							</asp:TemplateColumn>
							<asp:TemplateColumn HeaderText="Grade"  HeaderStyle-HorizontalAlign=Right  >
								<ITEMTEMPLATE>
									<asp:TextBox Runat="server"  Width="100" ID="txtAge" Text='<%# DataBinder.Eval(Container, "DataItem.Age") %>'  />
								</ITEMTEMPLATE>
							</asp:TemplateColumn>
						</Columns> 				 
						</Tittle:CustomDataGrid>
					</td>
					<td>&nbsp;&nbsp;</td>
					<td valign=top>
						<b><font color=red>Ex. II - Row(s)<font color=green>(1)</font> Freeze</font></b><br>
						<Tittle:CustomDataGrid id="dgTittle2" FreezeHeader=true
							runat="server" AutoGenerateColumns=True
							GridHeight="110"
							FreezeRows=1
							>						 
						</Tittle:CustomDataGrid>
					</td>
				</tr>
				<Td colspan=3><hr></Td>
				<tr>
				</tr>
				<tr>					
					<td valign=top>
						<b><font color=red>Ex. III - Column(s) <font color=green>(2)</font> Freeze</font></b><br>
						<Tittle:CustomDataGrid id="dgTittle3" FreezeHeader=true
							runat="server" 
							GridHeight="200"
							GridWidth="250"
							FreezeColumns=2
							AutoGenerateColumns=false							
							>				
							<Columns>
							<asp:TemplateColumn HeaderText="Name" >
								<ITEMTEMPLATE>
									<asp:Label Runat="server" ID="Label1" Width="100" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'  />
								</ITEMTEMPLATE>
							</asp:TemplateColumn>
							<asp:TemplateColumn HeaderText="Grade"  HeaderStyle-HorizontalAlign=Right  >
								<ITEMTEMPLATE>
									<asp:Label Runat="server"   ID="Textbox1" Text='<%# DataBinder.Eval(Container, "DataItem.Age") %>'  />
								</ITEMTEMPLATE>
							</asp:TemplateColumn>
							<asp:TemplateColumn HeaderText="Address"  HeaderStyle-HorizontalAlign=Right  ItemStyle-Wrap=False >
								<ITEMTEMPLATE>
									<asp:Label Runat="server"  ID="Label2" Text='<%# DataBinder.Eval(Container, "DataItem.Address") %>'  />
								</ITEMTEMPLATE>
							</asp:TemplateColumn>
						</Columns> 				 
						</Tittle:CustomDataGrid>
					</td>
					<td>&nbsp;&nbsp;</td>
					<td valign=top>
						<b><font color=red>Ex. IV - Add fixed row(s) <font color=green>(1)</font> at top in all datagrid page</font></b><br>
						<Tittle:CustomDataGrid id="dgTittle4" 
							runat="server" 							
							GridHeight="209"
							AutoGenerateColumns=false
							AddEmptyHeaders=1
							EmptyHeaderClass = "greyed"							
							PageSize=7
							FreezeHeader=true
							PagerStyle-Mode=NumericPages 
							AllowPaging=True 
							OnPageIndexChanged="dgTittle4_PageIndexChanged"
							>				
							<Columns>
							<asp:TemplateColumn HeaderText="Name" >
								<ITEMTEMPLATE>
									<asp:Label Runat="server" ID="Label3" Width="100" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'  />
								</ITEMTEMPLATE>
							</asp:TemplateColumn>
							<asp:TemplateColumn HeaderText="Grade"  HeaderStyle-HorizontalAlign=Right  >
								<ITEMTEMPLATE>
									<asp:Label Runat="server"   ID="Label4" Text='<%# DataBinder.Eval(Container, "DataItem.Age") %>'  />
								</ITEMTEMPLATE>
							</asp:TemplateColumn>
							<asp:TemplateColumn HeaderText="Address"  HeaderStyle-HorizontalAlign=Right  ItemStyle-Wrap=False >
								<ITEMTEMPLATE>
									<asp:Label Runat="server"  ID="Label5" Text='<%# DataBinder.Eval(Container, "DataItem.Address") %>'  />
								</ITEMTEMPLATE>
							</asp:TemplateColumn>
						</Columns> 				 
						</Tittle:CustomDataGrid>
					</td>
				</tr>
			</table>
		</form>
	</body>
</HTML>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Royal Bank of Scotland
India India
Total 11+ years of experience in Software Development. Expertise in .net (C#, asp.net, controls making, webservice, ado.net, xml/xsl, assemblies, rational XDE etc.) also UML, Design Patterns, ASP, Javascript, VbScript, Dhtml, CSS etc.

Member of Planet-Source-Code.com, www.brainbench.com and many other and have submitted several snippets/sample applications on the web.

Email:tittlejoseph@gmail.com

Comments and Discussions