GridView Sorting, Paging without using Session, ViewState or Cache


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.

Note:

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

Up Next
    Ebook Download
    View all
    Learn
    View all