DataGrid/GridView Paging and Sorting Using A DataReader

Ways to bind a DataGrid

Now most web apps simply need quick data retrieval presented to the client and again it's now good to discover that the DataGrid is not locked in with the DataSet, as we'll momentarily see.

There are some cool ways to bind a DataGrid that most people may not know about. I'll discuss two ways one could efficiently bind a DataGrid. The first really cool way to bind a DataGrid with is using a DataReader bound ArrayList , and the other way is using a standalone DataTable in conjunction with a DataView. Both will use the DataReader in getting our data, and both will have the cool paging and sorting features I mentioned as well. And, by the way, they're both cacheable, to further increase performance and scalability!

For all intents and purposes, the two methods that are going to be illustrated will be nearly identical, expect for the soon evident data creation/storage method, which is the aptly named GetDataReader subroutine listed below. The Arraylist in our example draws on the DataReader, but again in this instance to circumvent the error message above, I employ the DataReader and ArrayList to instance a new lightweight Structure Class (struct in C#) as opposed to a full fledged Class, to store our data in its properties, and this is what makes the difference in allowing you to bind a DataGrid with all it's features, instead of trying to natively bind a Datareader to a DataGrid. You can find more on Structures and Classes here - The Quick and Dirty .NET Guide to C#/VB Object-Oriented Programming. Additionally, the DataTable technique will also use the Datareader to create a new DataTable and a DataView to sort the data, and it's your typical run of the mill setup.

Notwithstanding, for brevity in this article I will discuss in detail the two aforementioned techniques solely and will not be discussing how to set up paging and sorting. For that have a look at Dynamic Column Sorting and Paging in ASP.NET for more detail. And also glance at .NET Data Caching to find out more insight on the caching methodologies discussed here.

So before we examine what the code does, I'll just list the entire code for you to have a good OnPreRender view of it, and break it down afterwards :-) We'll start off now with the ArrayList method code:

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.

“If debugging is the process of removing software bugs, then programming must be the process of putting them in.” - Edsger Dijkstra