Click here to Skip to main content
15,895,192 members
Articles / Web Development / ASP.NET

Generic GridView Using DataTable with Sorting and Paging

Rate me:
Please Sign up or sign in to vote.
4.11/5 (5 votes)
23 May 2009CPOL5 min read 58.2K   1.2K   17  
This extended control offers sorting and paging.
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class _Default : System.Web.UI.Page 
{

	#region TemplateMethods
	protected override void OnInit ( EventArgs e )
	{
		base.OnInit ( e );
		if ( this.IsPostBack == false )
		{
			//this.DataBind ( );
		} //only on post back
		else
		{
		} //eof else if is not postback

	} //eof OnInit


	protected override void CreateChildControls ( )
	{
		base.CreateChildControls ( );
		CreateDynamicControls ( );
	} //protected override void CreateChildControls ( )


	protected override object SaveViewState ( )
	{
		return new Pair ( base.SaveViewState ( ), null );
	}

	protected override void LoadViewState ( object savedState )
	{
		base.LoadViewState ( (( Pair )savedState).First );
		EnsureChildControls ( );
	} //LoadViewState

	protected void Page_Load ( object sender, EventArgs e )
	{   //comm -- the controls should be generated at the init stage and the databinding happens here

		this.DataBind ( );
		//debugOk Utils.Debugger.WriteIf ( "CHILD Page_Load END" );
	} //eof Page_Load


	#endregion //TemplateMethods

	private void CreateDynamicControls()
	{ 
		this.PopulateRds();
		for ( int i = 0 ; i<9 ; i++ )
		{

      Gui.Controls.XGrid gv = new Gui.Controls.XGrid ();
			gv.DataSource = Session ["dt"];
			gv.AllowSorting = true;
			gv.ShowResultSummary = true;
      gv.PageSize = 3; //how many items per page should be presented 
			gv.AllowPaging = true;
			gv.ID = i.ToString ( ) + i.ToString();
			gv.AutoGenerateColumns = true;
			gv.DataBind ( );
			panGvHolder.Controls.Add ( gv );
	
		} //eof for method CreateDynamicControls

	
	
	
	} //eof method CreateDynamicControls
	
	#region PopulationMethods
	//===================================================POPULATION METHODS START
	private void PopulateRds ( )
	{
		
		//PUT HERE YOUR CODE TO GET THE DATASET OBJ / DATATABLE OBJ
		DataSet rds = new DataSet ( );
		DataTable dt = new DataTable ( "myDynamicTable" );
		// Create a DataColumn instances

		DataColumn dId = new DataColumn ( );
		DataColumn dName = new DataColumn ( );
		DataColumn dDecimal = new DataColumn ( ); 

		dId.ColumnName = "Id";
		dId.DataType = Type.GetType ( "System.Int32" );

		dName.ColumnName = "Name";
		dName.DataType = Type.GetType ( "System.String" );


		dDecimal.ColumnName = "Decimal";
		dDecimal.DataType = Type.GetType ( "System.Double" );

		// Add these DataColumns into the DataTable
		dt.Columns.Add ( dId );
		dt.Columns.Add ( dName );
		dt.Columns.Add ( dDecimal );


		for ( int i = 0 ; i< 20 ; i++ )
		{

			DataRow dr = dt.NewRow ( );
			dr ["Id"] = i;
			dr ["Name"] = "Name " + System.Convert.ToString ( i );
			dr ["Decimal"] = 2222221.3333; 
			// Add the row into the table

			dt.Rows.Add ( dr );
		}
		rds.Tables.Add ( dt );
		
		Session [ "dt"] = rds.Tables [0];

		#region Old
		/*OldPopulate via Db
				string domainName = base.DomainName;
				string msg  = String.Empty;
				string tableName = "User_tb";
				Session [ base.PageName + ".tableName" ] = tableName;
				DbControllers.TableControl tc = new DbControllers.TableControl ();
				rds = new DataSet ();
				if (tc.GetAllFromTableOrView ( ref msg , ref domainName , ref rds , ref tableName ))
				{
						ds = GUI.GUIBuilder.GetEmptyMetaDataSet ( ref msg , ref tableName , 1 );
						Session [ base.PageName + ".ds" ] = ds;
						
						Utils.Debugger.DebugDataSet ( "From PopulateDataTable rds " , ref rds );
						Session [ base.PageName + ".rds" ] = rds;
						this.Master.ErrorLabel.ForeColor = System.Drawing.Color.Red;
				}
				else
				{

						this.Master.ErrorLabel.ForeColor = System.Drawing.Color.Red;
				}
				this.Master.ErrorLabel.Text = msg;
*/
		#endregion Old
	} //eof method PopulateDataTable


	#endregion //PopulationMethods 

} //eof _Default


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
Web Developer Oxit Oy
Finland Finland
I work in OXIT - a small IT consulting company, which has participated in the building of the most sophisticated IT Systems for several big Finnish and international companies (including Fortune 500 members) and continues to provide highly sophisticated IT Solutions to its customers.

I enjoy designing and implementing software or small scripts in different programming languages.

I am fascinated by the magic of software, which has the power to change the world.

Comments and Discussions