Paging in a GridView Web Server Control
The ASP.NET GridView control has a built-in paging capability that supports basic paging functionality. You can use the default paging user interface (UI) or create a custom paging interface.
How Paging Works in the GridView Control
The GridView control supports paging over the items in its data source. You set the AllowPaging property to true to enable paging. The GridView control supports paging in one of these ways:
If the GridView control is bound to a data source control that is capable of returning a single page of data when requested, the GridView control will take advantage of that capability directly. The number of rows requested may vary depending on the number of rows per page specified by the PageSize property, and whether the data source supports getting the total row count.
Note Of the data source controls that are included in the .NET Framework, only the ObjectDataSource control supports returning a single page of data.
Note If you are creating a data source (such as implementing a SelectCountMethod method in the source object for the ObjectDataSource control), it is strongly recommended that your data source return the total row count when supplying pages of data. This minimizes the number of records that the GridView control must request in order to retrieve a page of data. If the total row count is supplied by the source data object, the GridView control will request only a page of rows at a time. If the total row count is not supplied, the GridView control must request all rows from the data source (starting with the row that represents the requested page of data) and discard all rows except the row being displayed.
If the GridView control is bound to a data source control that does not support the paging capability directly, or if the GridView control is bound to a data structure in code through the DataSource property, the GridView control will perform paging by getting all of the data records from the source, displaying only the records for the current page, and discarding the rest. This is supported only when the data source for the GridView control returns a collection that implements the ICollection interface (including datasets).
Note If the data source does not support paging directly and does not implement the ICollection interface, the GridView control cannot page. For example, if you are using a SqlDataSource control and have set its DataSourceMode property to DataReader, the GridView control cannot implement paging.
Customizing the Paging Settings and User Interface
You can customize the paging user interface of the GridView control in a number of ways. You can set the size of the page (that is, the number of items to display at once) using the PageSize property. You can also set the current page of the GridView control by setting the PageIndex property. You can specify more custom behavior using either the PagerSettings property or by supplying a pager template.
Paging Modes
The PagerSettings property allows you to customize the appearance of the paging user interface (UI) that is automatically generated by the GridView control when you set the AllowPaging property to true. The GridView control can display direction controls that allow forward and backward navigation as well as numeric controls that allow a user to move to a specific page.
The PagerSettings property of the GridView control is set to a PagerSettings class. You can customize the paging mode by setting the Mode property of the GridView control. For example, you can customize the paging UI mode by setting it as follows:
GridView1.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast
The available modes are:
Pager Control Appearance
The GridView control has numerous properties that you can use to customize the text and images for different pager modes. For example, if you want to allow navigation using direction buttons but want to customize the text that appeared, you can customize the button text by setting the NextPageText and PreviousPageText properties, as in the following example:
GridView1.PagerSettings.NextPageText = "Click for next page"
GridView1.PagerSettings.PreviousPageText = "Click for previous page"
You can also use images to customize the appearance of your paging controls. The PagerSettings class includes image URL properties for the first, last, previous, and next page command buttons.
Finally, you can control the appearance of the paging commands by setting the PagerStyle property of the GridView control to a TableItemStyle value.
Data Paging Template
If you set the AllowPaging property of the GridView control to true, the GridView control automatically adds user interface (UI) controls for paging. You can customize the UI for paging by adding a PagerTemplate template. To specify which paging operation to perform, include a Button control with its CommandName property set to Page and a CommandArgument set to one of the following values:
First To move to the first page.
Last To move to the last page.
Prev To move to the previous page.
Next To move to the next page of data
A number To move to a specific page.
Paging Events
The GridView control raises two events when it moves to a new page of data. The PageIndexChanging event occurs before the GridView control performs the paging operation. The PageIndexChanged event occurs after the new page of data has been returned to the GridView control.
You can use the PageIndexChanging event to cancel the paging operation, if needed, or to perform a task before the GridView control requests a new page of data. You can use the PageIndexChanged event to perform a task after the user moves to a different page of data.