This documentation is archived and is not being maintained.

Specifying Paging Behavior in a DataGrid Web Server Control

Visual Studio .NET 2003

The DataGrid supports three types of paging:

  • Default paging with default navigation buttons
  • Default paging with custom navigation controls
  • Custom paging

For a discussion of these options, see Paging Behavior in DataGrid Web Server Controls.

Using Default Paging with Default Navigation Controls

To enable default paging, use the Property Builder to 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 handler that responds when users click a navigation control.

To specify paging using the Property Builder

  1. In Design view, select the DataGrid control, then click the Property Builder link at the bottom of the Properties window.
  2. In the DataGrid Properties dialog box, click the Paging tab.
  3. Select the Allow paging box. A complete set of default paging properties is enabled.
  4. Set the Page size property to the number of items to show on each page.
  5. In the Mode list, choose to display either Next, Previous buttons or buttons with Page numbers.
    • To display "Next" and "Previous" on the navigation buttons, set the Next page button text and Previous page button text properties. The default text on the button is "&gt;" for the next button and "&lt;" for previous button. At run-time, the symbols "<" and ">" are displayed.

      -or-

    • Set the Numeric buttons property to the number of pages to be displayed. For example, if you set Numeric buttons to 2 (that is, display two page numbers at a time), then the user may select from pages "...", 3, 4, and "..." when page 3 is displayed.
    • If you are using page numbers, set the PagerStyle object's PageButtonCount property to the number of buttons to display.

Each of the settings in the above procedure, "To specify paging using the Property Builder", corresponds to a property of the DataGrid. These properties may be set in the Page_Load event.

To specify paging programmatically

  1. Use the Page_Load event to enable paging and set the mode and button properties. The following example shows how you can set a DataGrid control called DataGrid1 to use simple paging and respond to a navigation request. The code sets the pager style to display five numbers at a time when the page is first displayed. When users click a navigation control, the code simply rebinds to the data source.
    ' Visual Basic
    Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
       'Put user code to initialize the page here
    
       With DataGrid1
          ' Enable paging.
          .AllowPaging = True
          ' Display 5 page numbers at a time.
          .PagerStyle.Mode = PagerMode.NumericPages
          .PagerStyle.PageButtonCount = 5
          .PageSize = 5
       End With
    
       If Not Page.IsPostBack Then
          DataGrid1.DataBind()
       End If
    End Sub
    
    // C#
    private void Page_Load(object sender, System.EventArgs e)
    {
       // Put user code to initialize the page here
       DataGrid1.AllowPaging = true;
       DataGrid1.PagerStyle.Mode = PagerMode.NumericPages;
       DataGrid1.PagerStyle.PageButtonCount = 5;
       DataGrid1.PageSize = 5;
    
       if (!Page.IsPostBack) 
       {
          DataGrid1.DataBind();
       }
    }
    
  2. Create an event handler for the grid's PageIndexChanged event to respond to a paging request. Use the e.NewPageIndex to update the DataGrid's CurrentPageIndex, then rebind the data.
    ' Visual Basic
    Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) _
    Handles DataGrid1.PageIndexChanged
       DataGrid1.CurrentPageIndex = e.NewPageIndex
       DataGrid1.DataBind()
    End Sub
    
    // C#
    private void DataGrid1_PageIndexChanged(object source, 
    System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
       DataGrid1.CurrentPageIndex = e.NewPageIndex;
       DataGrid1.DataBind();
    }
    

Using Default Paging with Custom Navigation Controls

One of the options on the Pager tab of the Property Builder is Show navigation buttons. If this option is not selected, no navigation controls are displayed. In this case, 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.

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 event handlers for your 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 following code shows how you can create code for a VCR-style navigation panel containing buttons that allow the user to go to the first, last, previous, or next page. The buttons are defined as Button controls whose CommandName property is set to indicate the navigation direction. All four buttons call the NavigationButtonClicked method when they are clicked.

    Note   In Visual C#, you can use the integrated development environment (IDE) to hook up the events and eliminate the
    +=
    assignments shown below. In this case, create the NavigationButtonClick method in the Code Editor. Return to the designer. Selecting each button in turn, select the Events tab in the Properties window. Select the NavigationButtonClick event from the drop-down list in the Click event.
    ' Visual Basic
    Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
       'Put user code to initialize the page here
       If Not Page.IsPostBack Then
          DataGrid1.DataBind()
       End If
    End Sub
    
    Private Sub NavigationButtonClicked(ByVal sender As Object, _
    ByVal e As EventArgs) Handles FirstPage.Click, PreviousPage.Click, NextPage.Click, LastPage.Click
       Dim direction As String = CType(sender, Button).CommandName
    
       Select Case direction.ToUpper()
          Case "FIRST"
             DataGrid1.CurrentPageIndex = 0
          Case "PREVIOUS"
             DataGrid1.CurrentPageIndex = _
                Math.Max(0, DataGrid1.CurrentPageIndex - 1)
          Case "NEXT"
              DataGrid1.CurrentPageIndex = _
                 Math.Min(DataGrid1.PageCount - 1, _
                 DataGrid1.CurrentPageIndex + 1)
          Case "LAST"
             DataGrid1.CurrentPageIndex = _
                 DataGrid1.PageCount - 1
          Case Else
       End Select
    
       DataGrid1.DataBind()
    End Sub
    
    // C#
    private void Page_Load(object sender, System.EventArgs e)
    {
       // Put user code to initialize the page here.
    
       // Have all the buttons hook up to the same event.
       First.Click += new System.EventHandler(this.NavigationButtonClick);
       Previous.Click += 
          new System.EventHandler(this.NavigationButtonClick);
       Next.Click += new System.EventHandler(this.NavigationButtonClick);
       Last.Click += new System.EventHandler(this.NavigationButtonClick);
    }
    
    private void NavigationButtonClick(object sender, System.EventArgs e)
    {
       string direction = ((Button)sender).CommandName;
    
       switch (direction.ToUpper())
       {
          case "FIRST" :
             DataGrid1.CurrentPageIndex = 0;
             break;
          case "PREVIOUS" :
             DataGrid1.CurrentPageIndex = 
                Math.Max(DataGrid1.CurrentPageIndex -1, 0);
             break;
          case "NEXT" :
             DataGrid1.CurrentPageIndex = 
                Math.Min(DataGrid1.CurrentPageIndex + 1, 
                DataGrid1.PageCount - 1);
             break;
          case "LAST" :
             DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1;
             break;
          default :
             break;
       }
       DataGrid1.DataBind();
    }
    

Creating Custom Paging

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. For information on custom paging, see DataGrid.AllowCustomPaging Property. For an example, see Walkthrough: Creating Paged Data Access Using a Web Forms Page.

See Also

Adding DataGrid Web Server Controls to a Web Forms Page | DataGrid Web Server Control | Introduction to the DataGrid Web Server Control

Show: