ASP.NET Core 5.0 : MVC, Razor Pages, Web API, EF Core, Blazor, Design Patterns, and more. Private online coaching for software developers. Click here for more details.

<%@ Page %>

Custom Paging In DataGrid


ASP.NET DataGrid has built-in functionality of paging. However, it has one disadvantage. Even though you are displaying only a small part of entire DataSet, you need to populate the DataSet with whole data. This works well when your DataSet is small but certainly not with huge amount of data. To overcome this problem DataGrid also allows custom paging. In custom paging you fetch only the data that is required to display the current page.


There are two properties of DataGrid that are important in this context:
  • AllowCustomPaging
  • VirtualItemCount
The first property needs no explanation. Note that AllowPaging property must also be True in addition to AllowCustomPaging. The VirtualItemCount property indicates the number of records that DataGrid will assume in the DataSet. The page numbers shown in the grid are dependent on this value. You may set it to any fictitious value or get total number of records in the DataSet had it been populated with whole data.

How to retrieve only the required number of records?

You can fetch only the required number of records by many ways. Some common ways are:
  • Using a stored procedure passing start and end record number
  • Using SELECT query with start and end record numbers in the where clause
  • Using SELECT TOP n clause

Sample Source Code

The sample source code available with this article contains a web form that shows how to implement custom paging for Employees table from Northwind database using SELECT query with where start and end record numbers (EmployeeID) clause.

Bipin Joshi is an independent software consultant, trainer, author, yoga mentor, and meditation teacher. He has been programming, meditating, and teaching for 24+ years. He conducts instructor-led online training courses in ASP.NET family of technologies for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.

Get connected : Facebook  Twitter  LinkedIn  YouTube

Posted On : 21 September 2002

Tags : ASP.NET Web Forms Server Controls Data Controls

Subscribe to our newsletter

Get monthly email updates about new articles, tutorials, code samples, and how-tos getting added to our knowledge base.


Receive Weekly Updates