Online courses in ASP.NET MVC, ASP.NET Core, and Design Patterns conducted by Bipin Joshi. Read more...
Learn ASP.NET MVC, ASP.NET Core, and Design Patterns through our online training programs. Courses conducted by Bipin Joshi on weekends. Read more details here.

<%@ 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 a yogi having 21+ years of experience in software development. He conducts online courses in ASP.NET MVC / Core, jQuery, AngularJS, and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced Yoga way of life he also teaches Ajapa Meditation to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 21 Sep 2002

Tags : ASP.NET Web Forms Server Controls Data Controls