Click here to Skip to main content
15,894,017 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.8K   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.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace FreezeHeader
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected Tittle.Controls.CustomDataGrid dgTittle;
		protected Tittle.Controls.CustomDataGrid dgTittle2;
		protected Tittle.Controls.CustomDataGrid dgTittle3;
		protected Tittle.Controls.CustomDataGrid dgTittle4;

	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here			
			if ( !Page.IsPostBack )
			{
				BindData(1); //first data grid.
				BindData(2); //second data grid.
				BindData(3); //iiird data grid.
				BindData(4); //ivth data grid.
			}
		}

		private void BindData(int gridno)
		{
			DataTable dt = new DataTable();
			dt.Columns.Add("Name");
			dt.Columns.Add("Age",typeof(System.Int32));				
			dt.Columns.Add("Address",typeof(System.String));				
			
			dt.Rows.Add(new object[] {"Peter",13,"1493 Meridan Drive"});			
			dt.Rows.Add(new object[] {"Paul",47,"5/67 Vaishali"});
			dt.Rows.Add(new object[] {"Abraham",56,"10/104 New Delhi GT road"});
			dt.Rows.Add(new object[] {"Andrew",59,"1/16 Barakhamba road"});
			dt.Rows.Add(new object[] {"Bernard",35,"1462 Meridan Drive"});
			dt.Rows.Add(new object[] {"Rozi",19,"1/16 Barakhamba road"});
			dt.Rows.Add(new object[] {"Ruby",25,"5/67 Vaishali"});
			dt.Rows.Add(new object[] {"Antony",27,"10/107 New Delhi GT road"});
			dt.Rows.Add(new object[] {"George",27,"1493 Meridan Drive"});
			dt.Rows.Add(new object[] {"Bobby",17,"4/96 Vaishali"});
			dt.Rows.Add(new object[] {"Barbara",40,"1/16 Barakhamba road"});
			dt.Rows.Add(new object[] {"Johnson",35,"99 Meridan Drive"});
			dt.Rows.Add(new object[] {"Mark",30,"1/16 Barakhamba road"});
			dt.Rows.Add(new object[] {"John",31,"1493 Meridan Drive"});
			dt.Rows.Add(new object[] {"Frank",27,"10/104 New Delhi GT road"});
			dt.Rows.Add(new object[] {"Alan",40,"5/67 Vaishali"});
			dt.Rows.Add(new object[] {"Tittle",28,"1493 Meridan Drive"});
			dt.Rows.Add(new object[] {"Joseph",62,"1/16 Barakhamba road"});			

			if ( gridno == 1 )
			{
				dgTittle.DataSource = dt.DefaultView;
				dgTittle.DataBind();
			}
			if ( gridno == 2 )
			{
				dt.DefaultView.Sort = "Age Desc";
				dgTittle2.DataSource = dt.DefaultView;
				dgTittle2.DataBind();
			}
			if ( gridno == 3 )
			{
				dt.DefaultView.Sort = "Name ASC, Age asc";
				dgTittle3.DataSource = dt.DefaultView;
				dgTittle3.DataBind();
			}
			if ( gridno == 4 )
			{
				dgTittle4.DataSource = dt.DefaultView;
				dgTittle4.DataBind();
			}
		}

		protected void dgTittle4_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
		{
			dgTittle4.CurrentPageIndex = e.NewPageIndex;
			BindData(4);
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}

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