April 2018 : Instructor-led Online Courses in ASP.NET MVC / Core and Design Patterns Read more...
Registration open for April 2018 batches of ASP.NET MVC and Design Patterns instructor-led online courses. Conducted by Bipin Joshi on weekends. Register today ! 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 a software consultant, trainer, author and yoga mentor having 22+ years of experience in software development. He also conducts online courses on ASP.NET MVC, ASP.NET Core and Design Patterns. 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 : Twitter  Facebook  Google+  LinkedIn

Posted On : 21 September 2002

Tags : ASP.NET Web Forms Server Controls Data Controls