January 2018 : Instructor-led Online Course in ASP.NET Core 2.0. Conducted by Bipin Joshi. Read more...
Registration for January 2018 batch of ASP.NET Core 2.0 instructor-led online course has already started. Conducted by Bipin Joshi. Register today ! Click here for more details.

<%@ Page %>

Custom Paging In DataGrid

Introduction

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.

Properties

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, an author and a yoga mentor having 22+ years of experience in software development. He also conducts online courses in ASP.NET MVC / 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 Meditation and Mindfulness 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