ASP.NET GridView Custom Paging

ASP.NET GridView Custom Paging

Asp.net GridView now supports custom paging too. Using custom paging the GridView will never slow down as it only fetches the records needed for the current page. With GridView custom paging support the developers can make their application much faster and light weight too.

GridView Paging VS GridView Custom Paging

Previously in GridView paging, it fetches all the records from the database then takes the current page records while discarding the rest. This approach was slow when the records are large and makes the application slower. To solve this problem GridView Custom Paging is used where only relevant records of a page is fetched.

Procedure to use Custom Paging in GridView

If you have used GridView before for binding data you can easily update your code with GridView custom paging Below we have given the procedure to help you do it.

1. Add the Custom Paging Stored procedure in your database. The work of this stored procedure is to fetch only the records of the current page of the GridView. This stored procedure is given at Custom Paging with SQL Server 2012

2. Add AllowCustomPaging=”true” to GridView in your .aspx page
Eg.

         
<asp:GridView ID="gridView" runat="server" AllowCustomPaging="true"  AutoGenerateColumns="false" PageSize="10" AllowPaging="True" OnPageIndexChanging="gridView_PageIndexChanging">
    <Columns>
    ...... 
    </Columns>
</asp:GridView>    

3. Set ‘VirtualItemCount’ property of GridView to the total number of records in the datasource.
Eg.

gridView.VirtualItemCount = totalRecords;

Note – totalRecords is an int variable. It contains the count of all the records in all the pages of the GridView.

4. Set the ‘DataSource’ property of GridView to the ‘DataTable’ which contains all the records of the current page. Then call ‘DataBind’ method.
Eg.

gridView.DataSource = myDataTable;
gridView.DataBind();

5. Create ‘PageIndexChanging’ event of GridView
Eg.

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gridView.PageIndex = e.NewPageIndex;
    Search(e.NewPageIndex + 1);
}

Note – We have called the Search() function whose work is to bind the next page records from the database.

Full Page Coding is given below

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        gridView.PageIndex = 0;
        Search(1);
    }
}

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gridView.PageIndex = e.NewPageIndex;
    Search(e.NewPageIndex + 1);
}

protected void Search(int pageNo)
{
    Int totalRecords;
    DataTable myDataTable = new DataTable();
    //Set total no of records in datasource in variable totalRecords
    //Set myDataTable with all the records of the current page from the database
    //Use pageNo variable of the function parameter to fetch the current page records from the database
		
    dataSet = dealDal.SearchDeal(dealBll);
    gridView.VirtualItemCount = totalRecords;
    gridView.DataSource = myDataTable;
    gridView.DataBind();        
}

Share this article -

yogihosting

ABOUT THE AUTHOR

This article has been written by the Technical Staff of YogiHosting. Check out other articles on "WordPress, SEO, jQuery, HTML" and more.

Enter your email address

Subscribe to this blog and receive notifications of new posts by email. Join over 81,000 other subscribers

Don't worry we won't spam.

subscribers