Library tutorials & articles
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:
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!
Related articles
Related discussion
-
High-Performance .NET Application Development & Architecture
by Manjot Bawa (0 replies)
-
An Introduction to VB.NET and Database Programming
by carlosmen (14 replies)
-
Using ADO.NET with SQL Server
by Manjot Bawa (23 replies)
-
HI All ! Request for some information on "Open Insight".
by PaulE (3 replies)
-
Hi this is Sajjan Hooda
by sajjanh (0 replies)
Related podcasts
-
ADO.NET Data Services in .NET 3.5 Service Pack 1 Beta1 with ASP.NET AJAX
Wally walks through using ASP.NET Podcast Show #114 - ADO.NET Data Services in .NET 3.5 Service Pack 1 Beta1 with ASP.NET AJAX.
Events coming up
-
Nov
18
15 Minutes of Fame
Dresher, United States
This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.
What about displaying, sorting etc' data in a DataGird/GridView with a paging mechanism using a scrollbar (not a Next/Previous buttons)??
It seems like no one has ever mention this issue.
Can anyone please help me solve this problem.
Regards ido
this site is really very good. i m really a big fan of this site thanks to all
developerfusion team
jitendra negi
Dimitrios thanks for the informative article. I have picked your code apart and tried out a few variants of your sample code which I am looking to implement into some of my web apps.
I tried a code sample in a web page that used “Data Grid Paging Using a Data Reader Bound Array List” – no caching or dynamic sorting.
This method still reads all the data even though a small fraction is only displayed on the page. This works well but when I blew the table up to 100,000 records (I was using MS Access in this experiment) it slowed down somewhat. Admittedly it was substantially faster than a similar sample of code using a dataset on the same database (as expected).
My question is;
Is there any way that straight forward data reader + data grid paging can be implemented in such a way that it will only get the records from the database that the webapp actually displays on the page? Any suggestions, directions or web links would be appreciated.
I'm working on an app that implements a structure and stores a recordset in an ArrayList based upon the example outlined here. I used the same to bind to a DataGrid and it worked out so well, I thought it might be a better solution that moving back and forth through a DataSet to perform some simple though numerous calculations.
In order to perform these calcs, I have to compare elements of the array checking for duplicates on some columns and then just comparing other elements to strings utilizing IndexOf and LastIndexOf. Unfortunately I haven't been able to get it to work. On another site I read that those 2 methods use Object.Equals and as the array contains structure objects, I need to override the Equals function. That route has been just as unsuccessful.
The code below generates an that the index is out of range on the line that assigns the array item to 'last' because the LastIndexOf search returns -1. Thing is the stored procedure I use to retrieve the recordset guarantees that there would be a match on first.account. Can anyone point me in the right direction?
Public Overrides Function Equals(ByVal obj As Object) As Boolean If obj Is Nothing Or Not Me.GetType() Is obj.GetType() Then Return False End If Dim d As duplicates = CType(obj, duplicates) Return Me.account = d.account End Function'Errant code below
conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader() If reader.HasRows Then 'Create array to hold entries for accounts tracked more than once per LOB Dim arrResults As New ArrayList Dim dupEntries As New duplicates While reader.Read With arrResults.Add(
New duplicates(reader.GetValue(0), reader.GetValue(1), reader.GetValue(2), _reader.GetValue(3), reader.GetValue(4), reader.GetValue(5), reader.GetValue(6), _
reader.GetValue(7), reader.GetValue(8), reader.GetValue(9), reader.GetValue(10), _
reader.GetValue(11), reader.GetValue(12), reader.GetValue(13), reader.GetValue(14)))
End With End Whilereader.Close()
Dim first As duplicates Dim last As duplicates Dim i As Integer For i = 0 To arrResults.Count - 1first =
CType(arrResults.Item(i), duplicates)Trace.Warn(arrResults.LastIndexOf(first.account))
last = arrResults.Item(arrResults.LastIndexOf(first.account))
Trace.Warn(
"first date" & first.tDate)Trace.Warn(
"last date " & last.tDate) Next End IfYeah thanks. No, it does absolutely work, regardless, and even though the "AllowCustomPaging" property isn't part of the Gridview, it does not affect it, as I just copied the code from the article and it ran perfectly. The Gridview simply ignores it, whether set to "True" or "False"
I would check and make sure it's not something else you may have modified.
Hope this helps
Good article overall. But the Gridview version cannot work. One problem is it shows the following property:
AllowCustomPaging="False"
that is not available in Gridview.
Hello...
Can anybody plis post the C# version?
Thx in advance...
Sup everyone...
I have a datagrid and I want to implement a button that will simulate the datagrid scroll down click button. What events do I need to handle? How to make my datagrid rows to scroll once my button is pressed?
Hello
The DataReaders read one-way data As I undesrtand you dynamically bind your Oracle Database to your Datagird. Try to convert oracleDataReader to DataView. DataViews can be red 2 way and they are Compatible with AllowPaging=true
I have recently sent a Control to Asp.Net I think it is in the proccessing stage that Control converts OdbcDataReader to DataView below is the open code for you to give you an idea. You must apply the same logic to your Oracle Reader
Good Luck,
Baris ERGUN
www.thecoreopsis.com
public static DataView ConvertToDataView(OdbcDataReader setToCheck, string tableName)
{
DataTable dataReaderTable = new DataTable(tableName);
try
{
for(int h=0;h<setToCheck.FieldCount;h++)
{
DataColumn temp = new DataColumn(setToCheck.GetName(h),setToCheck.GetFieldType(h));
dataReaderTable.Columns.Add(temp);
}
while(setToCheck.Read())
{
DataRow dr = dataReaderTable.NewRow();
for(int g=0;g<setToCheck.FieldCount;g++)
{
dr
= setToCheck.GetValue(setToCheck.GetOrdinal(setToCheck.GetName(g)));
}
dataReaderTable.Rows.Add(dr);
}
return dataReaderTable.DefaultView;
}
catch
{
return null;
}
}
This thread is for discussions of DataGrid/GridView Paging and Sorting Using A DataReader .