65.9K
CodeProject is changing. Read more.
Home

Paging control for Gridview Display

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.61/5 (9 votes)

Mar 20, 2007

1 min read

viewsIcon

44141

downloadIcon

701

A simple paging control for grid view display

Screenshot - pagingGrid.jpg

Introduction

Paging Control for Gridview enables the paging control to your gridview display. Large number of data when displayed on a single screen could be clumpsy. With the paging enabled on the grid view, total data could be divided into number of pages and displayed as separate pages.


Using the code

The simple logic of paging is as follows: we maintain a global datatable, current index and a temporary datatable. First the total data to be displayed is loaded in the main Datatable. This will be done only once for a particular set of data. After the main datatable is loaded, then according to the provided no. of documents for each page, total page no. is determined and the each page is shown with the corresponding data from the main datatable.

//
public int currentIndex = 1; 
public DataTable mainDTable;
public DataTable tempDTable;
public int numDocs;
public int totPages = 1;
//
For this a temporary data table is maintained. What it does is, it fetches the selected rows from the main datatable according to the current index and the total no. of documents to be shown in each page. The grid view is cleared each time a page changes and is reloaded each time from the temp. data table which keeps on changing with the current index. The range for the data to be populated in the temp datatable is calculated as shown above as from_value and to_value. so at each instance the temp datatable consist of data from the values of fromvalue to the tovalue.
//
int fromValue = numDocs * (currentIndex - 1) + 1;
int toValue = currentIndex * numDocs;
//

Points of Interest

The important thing to remember here is the tracking of the current index. If you get it slipped then whole the logic may be useless.