Days are over when developers had to write complex logic for implementing paging in an ASP page. With DataGrid control in ASP.NET, paging has become far simpler than it was in earlier ASP days. Using DataGrid control we can implement paging in variety of ways. This document discusses various ways in which we can implement the same.
DataGrid control supports two kinds of paging i.e. Default Paging and Custom Paging.
Default Paging:
To display the data in a DataGrid Control, we need to provide a data source and bind the DataGrid with the data source as shown below.
//Declaring DataGrid
protected System.Web.UI.WebControls.DataGrid DataGrid1;
//Binding DataGrid with a Data Source (DataSet in our Case)
DataGrid1.DataSource = dataSet1;
DataGrid1.DataBind();
To enable default paging we need to explicitly set AllowPaging property of DataGrid control to True. Use PageSize property to set the number of records to be displayed per page. PageSize has a default value of 10. DataGrid control has a property CurrentPageIndex which gives the index of current page being displayed i.e. for the first Page DataGrod1.CurrentPageIndex will be zero. Another property PageCount exposes the total number of pages. In other words PageCount indicates the number of pages in DataGrid control to display all the data from the data source. We will make use of above properties to implement paging in our ASPX page. When the user clicks on a page hyperlink on the ASPX web form, postback operation is performed. On postback, PageIndexChanged Event of DataGrid is called. What all we need to do is implement an eventhandler for the PageIndexChanged event as follows.
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.GridPageIndexChanged);
//Implement the EventHandler
private void GridPageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
//Bind the DataGrid again with the Data Source
DataGrid1.DataSource = dataSet1;
DataGrid1.DataBind();
}
So Paging is implemented in out ASPX page. Isn't it easy then what we use to do in ASP?
Use Mode property of DataGrid Control to set the type of paging i.e. next previous mode or page number mode.
Mode has two options i.e. NextPrev or NumericPages. While using NextPrev Mode, we can use NextPageText and PrevPageText properties to set the text displayed for next and previous link. While using NumericPages Mode, we can use PageButtonCount property to set the number of links displayed i.e. if we set the PageButtonCount property to 5 then at any point of time, link for only 5 pages will be displayed along with ellipsis (...).
Apart from above, we can use properties like BackColor, ForeColor, Font, HorizontalAlign, Position and Width to set the visual display of DataGrid Page.
Note: In order to implement default paging in a DataGrid control, the data source must implement ICollection interface for example DataSet.
Custom Paging:
One problem with Default Paging is, every time we request for a page in the DataGrid, it retrieves complete data from the database. This problem can be a bottle neck if the volume of date we are working with is huge. A genuine suggestion to this problem which comes into mind immediately is to fetch only that data which we need to display on the page. This is what custom paging is all about i.e. fetching only required data from database. To enable Custom paging we need to explicitly set AllowCustomPaging property of DataGrid control to True. Use PageSize property to set the number of records to be displayed per page. We need to set VirtualItemCount property of DataGrid control. This property tells the DataGrid control about the total number of records that we need to page thru.
Dung DinhPosted May 23, 2012, 9:53 AM
protected void DataGrid1_PageIndexChanging(object sender, GridViewPageEventArgs e) { DataGrid1.PageIndex = e.NewPageIndex; DataGrid1.DataSource = dataSet1; DataGrid1.DataBind(); }
SmiteditedPosted Aug 2, 2010, 8:43 AMEdited Aug 2, 2010, 8:51 AM
hi amit, will you please send me a code; how to implement it with asp.net on my id : [email protected] thanks smit
naveen guptaPosted Feb 20, 2010, 5:34 AM
is this datagrid is a third party tool that u r using because i am not getting the virtualitemcount property of the standard gridview control in asp.net . please reply ASAP.
Ozgur ONERPosted May 3, 2009, 11:45 AM
It really helped alot! Thank you dude...
vazra yPosted Mar 29, 2009, 1:30 AM
explaination on paging ws good bt it could be more better if example ws included in it