DataGrid/GridView Paging and Sorting Using A DataReader

Introduction

In a previous article I had written on Dynamic Column Sorting and Paging in ASP.NET, I demonstrated one of nine ways one could page and bi-directionally sort a DataGrid. I opted for the old hidden field method. At any rate, this method, among the eight others works fine, and it all depends on how you want to approach this based on any given factors in your app. Having said that, the problems we'll be discussing here lay not with either the paging or sorting aspects of it, but rather in implementing the most efficient manner in retrieving data and presenting it to the client.

No one wants to bog down a server with unnecessary network traffic by displaying hundreds or even thousands of rows or data, nor in furthermore weighing down server resources by creating any unnecessary memory demanding objects, when you can easily conserve resources and time by utilizing more lightweight data objects to display data with.

As you may or may not realize, there are numerous ways to bind a DataGrid. The most common way is the typical DataAdapter/DataSet combo. Although, this method has its proper setting and time to be used, you'll need to realize that aside from data modification or management, employing this strategy will prove in hindering performance, as the DataSet is definitely slower that the lightning fast read-only, forward-only DataReader. Some performance statistics worth mentioning can be found here - Performance Comparison: Data Access Techniques.

For some real good further reading can be found here Why I Don't Use DataSets in My ASP.NET Applications and More On Why I Don't Use DataSets in My ASP.NET Applications.

Now, the Datareader is forward-only right? Hmm, but what about paging and sorting? Can we natively implement a DataReader to provide paging and sorting as one would typically with a DataSet? The answer unfortunately is no, and if you try doing this you receive this error message:

AllowCustomPaging must be true and VirtualItemCount must be set for a DataGrid with ID DataGridID when AllowPaging is set to true and the selected datasource does not implement ICollection.

It is after all a foward-only cursor type object. However, there are ways around this and in this article I will demonstrate how you could not only use the DataReader to bind a DataGrid, but cache it, page it and persistently and bi-directionally sort it!

You might also like...

Comments

About the author

Dimitrios Markatos

Dimitrios Markatos United States

Dimitrios, or Jimmy as his friends call him, is a .NET developer/architect who specializes in Microsoft Technologies for creating high-performance and scalable data-driven enterprise Web and des...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.” - Cory Doctorow