.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

GridView Sorting, Paging without using Session, ViewState or Cache etc...

Posted By:Suthish Nair       Posted Date: August 29, 2010    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

GridView Sorting, Paging without using Session, ViewState or Cache etc...

I was wondering why most of the web sites having articles/blogs about GridView Sorting, Paging uses ViewState/Sessions objects etc for storing datatable records for further use. Another way of using was getting data from database on every request. But, if DB table data is not frequently changing, then why we need to hit DB every time. Why can't' we store the data somewhere else. Here, again came ViewState/Sessions objects etc for saving. Is there no other way of doing it? Yes, we can achieve it by using static classes.



Am trying to show how we can do it by using static classes, if my way of coding and logics is not good, please comment on it, so improvement can be done. Also, this minimizes number of coding lines.



MVS used: 2008/2010

DB used: Northwind


  1. Declared a temp Dataset object è private static DataSet tmpds = new DataSet();, which was used for storing records from Database and later used for sorting and paging of GridView.
  2. For sorting, declared a temp String è private static String tmpSort = " DESC";, and later using as

dataView.Sort =

e.SortExpression + (string)((e.SortDirection == SortDirection.Ascending) ? tmpSort : tmpSort);

tmpSort = (tmpSort == " ASC") ? " DESC" : " ASC";


You can see on Page Load am calling Timer event for fetching the records. Also, Timer Interval property was set to 10secs. It will trigger the event in every 10secs for fetching records from DB; this will take care of new records updations also.

Sample source codes attached for download and please post your comments.

 Subscribe to Articles


Further Readings:

Author: Deco         Company URL: http://www.dotnetspark.com
Posted Date: September 11, 2010


It's really a nice piece of code.

Thank you very much fior sharing this in Dotnet Spark!!!
Author: sam         Company URL: http://www.dotnetspark.com
Posted Date: September 17, 2010

This one works. But it is not a good solution. Imagine several user asses your web application at the same time.
That means, data set is overwriting every time when a user is accessing the page.
I solve this problem by implementing the following solution.

The table structure

user |code |product |Date
Lema Begebeya |28558 |SBG |06.04.2005
Lemana Zemedochu |531615 |104 ZZ |23.05.2005
Abbebe Gutema |684481 |GT 5241 |01.06.2005
Samson Balemu |531615 |ZZP01 |28.08.2005
Gerhard LORENZ |684481 N |T0 01 |26.09.2006

protected void GridViewUser_Sorting(object sender, GridViewSortEventArgs e)

DataTable dataTableTemp = new DataTable();

// define the table''''s schema
dataTableTemp.Columns.Add(new DataColumn("user", typeof(string)));
dataTableTemp.Columns.Add(new DataColumn("code", typeof(string)));
dataTableTemp.Columns.Add(new DataColumn("product", typeof(int)));
dataTableTemp.Columns.Add(new DataColumn("date", typeof(string)));

int CellCount = GridViewUser.HeaderRow.Cells.Count - 1;

foreach (GridViewRow row in GridViewUser.Rows)
//Create the records
DataRow dataRow = dataTableTemp.NewRow();

for (int i = 0; i < CellCount; i++)
dataRow[i] = Page.HtmlDecode(row.Cells[i + 1].Text); //convert HTML Unicode to plain text


// Sorting direction should be placed on the web page as hidden field
if (HiddenFieldSortDirection.Value == " ASC")
HiddenFieldSortDirection.Value = " DESC";
HiddenFieldSortDirection.Value = " ASC";

dataTableTemp.DefaultView.Sort = e.SortExpression + HiddenFieldSortDirection.Value;

GridViewUser.DataSource = tableView.DefaultView.ToTable();

Author: Suthish Nair         Company URL: http://www.dotnetspark.com
Posted Date: September 17, 2010

Hi Sam,

Above article is not for user-specific logics. You are right, the article not saying this a final solution to avoid session, veiwstate etc .But to an extend you can avoid those. If you are sharing same datatable/dataset across all users/sessions, then you can try above article. Static variable is not good to store user-specific data.

Still your code does not work for user-specific logics, because every time the dataset will get refreshed on new page request.

This line will always reset the dataset and bind new updated records if needed.

private static DataSet tmpds = new DataSet();

There will be always one copy of tmpds. I think this will not increase the variable/obects count in memory, only the dataset will get refreshed with new records. If am wrong, please correct me.

But, if your datatable/dataset is different against every sessions then i will not suggest above article.

Also, you can change your logic in removing static variable (tmpSort) and use a Label control for storing sorting value.

<asp:Label ID="lbl1" runat="server"> DESC</asp:Label>
//private static String tmpSort = " DESC";
dataView.Sort = e.SortExpression + (string)((e.SortDirection == SortDirection.Ascending) ? lbl1.Text : lbl1.Text);
lbl1.Text = (lbl1.Text == " ASC") ? " DESC" : " ASC";

A Static variable has only a single lifetime, which lasts for the entire time your application is running.
Resetting of application pool or IIS reset will helps clear the data.

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend