Paging Through Records in a DataGrid
The DataGrid control has a built-in support for paging through the records of a data source. This is an important feature because it helps to display huge data . Paging can be enabled only if a DataGrid control's data source implements the ICollection interface. This interface is not implemented by a DataReader, so a DataTable is used.
The following example demonstrates the Paging: -
The output of above example is shown below:
The DataGrid above displays five records at a time from the Titles database table.
By clicking the < and > links displayed at the bottom of the DataGrid, one page forward or backward can be moved.
The number of records displayed in a page is determined by the DataGrid control's PageSize property. By default, this property has the value 10. It is set to 4 in the listing to display fewer records per page.
A new page is selected with the dgrdTitles_PageIndexChanged subroutine. This subroutine assigns the value of the NewPageIndex to the DataGrid control's CurrentPageIndex. The subroutine then rebinds the DataGrid to the data source, displaying the new page of records. The appearance of the page links canbe controlled by modifying the properties of the DataGridPagerStyle object.