asp.net.ph

Skip Navigation Links

Specifying Paging Behavior in a DataGrid Control

Controls You Can Use on Web Forms   ASP.NET Data Controls   DataGrid Control


The DataGrid Web server control enables paging thru data in the grid using any of these methods:

Method Description
Default paging using built-in paging controls The grid displays navigation buttons in its pager element, which can be either Next and Previous buttons or page numbers. When the user clicks these, the grid updates the current page number and raises an event that you can use to refresh the data.
Default paging using custom navigation controls Similar to default paging but enables use of custom paging controls. In this case, you determine and specify the current page of the grid in the click events for these buttons. The control then selects the appropriate data rows to display.
Custom paging You provide your own navigation controls, manually set which page to display, and provide the rows for just that page. This option allows you to move any number of pages at a time, jump to a specific page, and so on.

Automatic versus Manual Paging

The page displayed by the grid is determined by its CurrentPageIndex property. The built-in controls set this property automatically; if you are providing custom navigation controls, you set this yourself. After the CurrentPageIndex is set, the grid should be re-bound to the data source. The grid will recreate the entire data set and automatically move to the appropriate place in the data set. It then displays enough rows to make up one page of the grid.

If you are working with a large data set, recreating the entire data set each time users navigate to a new page can degrade performance. In this case, you might prefer to get data in page-size "chunks" — that is, retrieve just a page worth of records at a time. To do that, you turn off the automatic paging feature of the grid so that it does not assume that it is working with the entire data set. You then manually get the correct number of rows to fill the grid.

For details, see Creating Manual Paging below.

Using the Built-In Paging Controls

To use default paging, you set properties to enable paging, set the page size, and specify the style of the paging controls. Paging controls are LinkButton controls. You can choose from these types:

  • Next and previous buttons. The button captions can be any text you want.
  • Page numbers, which allow users to jump to a specific page. You can specify how many numbers are displayed; if there are more pages, an ellipsis ( ... ) is displayed next to the numbers.

You must also create an event-handling method that responds when users click a navigation control.

To use the built-in paging controls

  • Set the control’s AllowPaging property to true.
  • Set the PageSize property to the number of items to display per page.
  • To set the appearance of the paging buttons, include a <PagerStyle> element into the page as a child of the DataGrid control. For syntax, see DataGrid Control Syntax.
  • Create a handler for the grid’s PageIndexChanged event to respond to a paging request. The DataGridPageChangedEventsArgs enumeration contains the NewPageIndex property, which is the page the user would like to browse to. Set the grid’s CurrentPageIndex property to e.NewPageIndex, then rebind the data.

The following code snippet shows the typical logic for the PageIndexChanged event handler.

void setPage ( object src, DataGridPageChangedEventArgs e ) {
   // set CurrentPageIndex to the page the user clicked.
   myGrid.CurrentPageIndex = e.NewPageIndex;
   // fetch and rebind the data.
   BindGrid ( );
}
  C# VB
DataGridAllowPaging.aspx
Run Sample | View Source

Providing Custom Navigation Controls

You can provide your own navigation buttons and manipulate the CurrentPageIndex property of the DataGrid. The DataGrid will still take care of breaking the data source into appropriate pages and displaying the selected page.

When you create manual paging, you can use any way you like to set the pager style that the grid should display.

To provide custom navigation controls

  1. Add server controls to the Web Forms page that the user can use to navigate. For example, you may want to create ImageButton controls with forward and reverse images on them.
  2. In the event handlers for the navigation controls, set the DataGrid control’s CurrentPageIndex property to the page to go to, and then rebind the grid to the data source.

The below snippet shows how you can create code for a custom navigation panel that allow the user to go to the first, last, previous, or next page. The paging elements are defined as <asp:Button> controls whose CommandArgument property is set to indicate what page they go to. All four buttons call the following method when they are clicked.

void setPageCustom ( object src, EventArgs e ) {
   // used by custom paging UI
   string direction = ( ( Button ) sender ).CommandArgument;

   switch ( direction ) {
      case ( "first" ) :
         myGrid.CurrentPageIndex = 0;
         break;
      case ( "prev" ) :
         if ( myGrid.CurrentPageIndex > 0 )
            myGrid.CurrentPageIndex --;
         break;
      case ( "next" ) :
         if ( myGrid.CurrentPageIndex < ( myGrid.PageCount - 1 ) )
            myGrid.CurrentPageIndex ++;
         break;
      case ( "last" ) :
         myGrid.CurrentPageIndex = ( myGrid.PageCount - 1 );
         break;
   }
   BindGrid ( );
}
  C# VB
CustomPagingControls.aspx
Run Sample | View Source

Creating Manual Paging

By default, the DataGrid control recreates the data set each time the user navigates to a new page and then calculates which rows to display for the requested page. This may not be efficient, however, in applications that need to access large data sets.

By using the AllowCustomPaging property of the DataGrid, you can have complete control over which records are displayed. Custom paging improves performance by reducing the amount of data moved around in the system, since you can retrieve just one page of data at a time from the data source.

To do so, you turn off automatic paging, then take responsibility in your own code to get just the data for a single page.

The following examples demonstrate two different ways of using the AllowCustomPaging property to enable custom paging. One uses an autoincrement data model, and the other uses a data source with primary keys.

Custom Paging with AutoIncrement Data Model
Run Sample | View Source
Custom Paging Using Primary Key Values
Run Sample | View Source



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note