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 %>

Alphabetical Paging in DataGrid

Introduction

All of you must be familiar with paging features of ASP.NET DataGrid. The built-in paging is based on number of records per page. In certain application displaying just the certain number of rows per page is not sufficient. Consider for example that you are building a contact manager or address book. Typically such data is accessed alphabetically. All the records are required to be indexed as per alphabets i.e. all records starting with A should be displayed as a single page and so on. This article tells you how to do such alphabetical paging in DataGrid.

Setting DataGrid Footer

In order to page the DataGrid based on alphabets, we need to display alphabets instead of normal page numbers. This can be done by adding link buttons in the DataGrid footer. You may add the link buttons at design time or at run time. The later approach is more flexible and gives more control on which alphabets you want to display. You can add link buttons at run time by using DataGrid1_ItemCreated event. You also need to use CommandName and CommandArgument properties of the link button to identify them uniquely.

Handling the Paging

When user clicks on any of the alphabet, the form is posted back and DataGrid1_ItemCommand event will be raised. Inside the event you can take action based on the CommandName and CommandArgument of the link button that caused the event to be raised. Typically this action will be to fetch records for the specified alphabet and bind the DataGrid again.

Source Code and Screen Shot

Full source code of the article is available in the download accompanying the article as VS.NET project. Following figure shows sample run of the application.


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 : 17 August 2002


Tags : ASP.NET Web Forms Server Controls Data Controls