This documentation is archived and is not being maintained.

GridView.PageIndex Property

Gets or sets the index of the currently displayed page.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public virtual int PageIndex { get; set; }
<asp:GridView PageIndex="Int32" />

Property Value

Type: System.Int32
The zero-based index of the currently displayed page.


The PageIndex property is set to a value less than 0.

When the paging feature is enabled (by setting the AllowPaging property to true), use the PageIndex property to determine the index of the currently displayed page. You can also use this property to programmatically change the displayed page.

The following example demonstrates how to use the PageIndex property to determine the index of the currently displayed page in the GridView control. The example also shows how the PageIndex property can be used to specify which page is displayed after the user selects a value from the pager row.

<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<script runat="server">

  protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)

    // Retrieve the pager row.
    GridViewRow pagerRow = CustomersGridView.BottomPagerRow;

    // Retrieve the PageDropDownList DropDownList from the bottom pager row.
    DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");

    // Set the PageIndex property to display that page selected by the user.
    CustomersGridView.PageIndex = pageList.SelectedIndex;


  protected void CustomersGridView_DataBound(Object sender, EventArgs e)

    // Retrieve the pager row.
    GridViewRow pagerRow = CustomersGridView.BottomPagerRow;

    // Retrieve the DropDownList and Label controls from the row.
    DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
    Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");

    if(pageList != null)

      // Create the values for the DropDownList control based on  
      // the  total number of pages required to display the data 
      // source. 
      for(int i=0; i<CustomersGridView.PageCount; i++)

        // Create a ListItem object to represent a page. 
        int pageNumber = i + 1;
        ListItem item = new ListItem(pageNumber.ToString());         

        // If the ListItem object matches the currently selected 
        // page, flag the ListItem object as being selected. Because 
        // the DropDownList control is recreated each time the pager 
        // row gets created, this will persist the selected item in 
        // the DropDownList control.    
          item.Selected = true;

        // Add the ListItem object to the Items collection of the  
        // DropDownList.



    if(pageLabel != null)

      // Calculate the current page number. 
      int currentPage = CustomersGridView.PageIndex + 1;     

      // Update the Label control with the current page information.
      pageLabel.Text = "Page " + currentPage.ToString() +
        " of " + CustomersGridView.PageCount.ToString();




<html xmlns="" >
  <head runat="server">
    <title>GridView PagerTemplate Example</title>
    <form id="form1" runat="server">

      <h3>GridView PagerTemplate Example</h3>

      <asp:gridview id="CustomersGridView" 

        <pagerstyle forecolor="Blue"


          <table width="100%">                    
              <td style="width:70%">

                <asp:label id="MessageLabel"
                  text="Select a page:" 
                <asp:dropdownlist id="PageDropDownList"


              <td style="width:70%; text-align:right">

                <asp:label id="CurrentPageLabel"





      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0