Introduction
- Here, I will explain how to set custom page size and paging for ASP.NET GridView.
Why We Need To Use Custom Page Size And Paging
- We utilize default GridView paging, which means we are getting all the records from the database but we use Custom paging, which means we are getting a small amount of records (like 10 records).
- For example, we have 20000 record in the database. We assume that 20000 data binds to GridView, then we set page size as 10. if we are not using custom paging GridView it means that the full 20000 data binds to GridView, but this process takes a lot of time because the data volume is high, so we decrease fetching time by using custom paging GridView.
Stored Procedure For Paging
- The procedure, mentioned below, is used to fetch the page size, which is based on the records.
- I set default page size as 10 so 10 records only return this procedure.
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- -- === === === === === === === === === === === === === === === CREATE PROCEDURE GetCustomersPageWise
- @PageIndex INT = 1,
- @PageSize INT = 10,
- @RecordCount INT OUTPUT
- AS
- BEGIN
- SET NOCOUNT ON;
- SELECT
- ROW_NUMBER() OVER(ORDER BY[CustomerID] ASC) AS RowNumber,
- [CustomerID],
- [CompanyName],
- [ContactName]
- INTO #Results FROM[Customers]
- SELECT @RecordCount = COUNT( * )
- FROM #Results
- SELECT *
- FROM #Results
- WHERE RowNumber BETWEEN(@PageIndex - 1) * @PageSize + 1
- AND(((@PageIndex - * @PageSize + 1) + @PageSize) - 1
- DROP TABLE #Results;
- END
- GO
HTML code
Sample ASP.NET Front end code for custom grid is given below.


Join the conversation! Your thoughts help the community grow.