分页使您可以按页段显示 DataGrid 控件的内容。页上的项数由 PageSize 属性确定。如果没有为 PageSize 属性指定任何值,则 DataGrid 将在一页上显示 10 项。通常,在每次 DataGrid 控件移到不同页时都将加载包含 DataGrid 控件中每一行的数据源。当数据源很大时这会占用大量资源。自定义分页使您可以只加载显示单页所需的数据段。若要启用自定义分页,请将 AllowPaging 和 AllowCustomPaging 属性都设置为 true。接下来,提供用来处理 PageIndexChanged 事件的代码。PageIndexChanged 事件处理程序的典型逻辑是首先将 CurrentPageIndex 属性设置为您要显示的页的索引。注意   事件处理程序接收 DataGridPageChangedEventArgs 对象作为参数。可以使用该参数的 NewPageIndex 属性来确定用户从 DataGrid 控件的页选择元素中所选择的页的索引。
接下来,创建包含要在单页显示的数据的数据源,然后使用 DataBind 方法将数据绑定到 DataGrid 控件。注意   因为只加载一个数据段,所以您必须将 VirtualItemCount 属性设置为 DataGrid 控件中各项的总数。这使得该控件可以确定显示 DataGrid 控件中每一项所需的总页数。只要确定了 DataGrid 控件中的总项数,通常就可以用编程的方式设置此属性。
在通过将 AllowCustomPaging 属性设置为 false 来启用分页后,DataGrid 控件假设数据源包含所有要显示的项。DataGrid 控件根据 CurrentPageIndex 属性指定的页索引以及 PageSize 属性指定的页上项的数目来计算显示的页上各项的索引。当 AllowCustomPaging 属性被设置为 true 时,DataGrid 控件假设数据源只包含由 VirtualItemCount 属性确定的项。所有项(项数最多可达由 PageSize 属性指定的数目)都将显示出来。示例
下面的示例展示如何使用 AllowCustomPaging 属性来启用自定义分页。[C#] 
<%@ Import Namespace="System.Data" %><html>
<script language="C#" runat="server">   int start_index;   ICollection CreateDataSource() 
   {      DataTable dt = new DataTable();
      DataRow dr;      dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
      dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
      dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));      for (int i = start_index; i < start_index + ItemsGrid.PageSize; i++) 
      {
         dr = dt.NewRow();         dr[0] = i;
         dr[1] = "Item " + i.ToString();
         dr[2] = 1.23 * (i+1);         dt.Rows.Add(dr);
      }      DataView dv = new DataView(dt);
      return dv;
   }   void Page_Load(Object sender, EventArgs e) 
   {      if (CheckBox1.Checked)
         ItemsGrid.PagerStyle.Mode=PagerMode.NumericPages;
      else
         ItemsGrid.PagerStyle.Mode=PagerMode.NextPrev;      if (!IsPostBack)
      {
         start_index = 0; 
         ItemsGrid.VirtualItemCount=100; 
      }      BindGrid();
      
   }   void Grid_Change(Object sender, DataGridPageChangedEventArgs e) 
   {
       
      ItemsGrid.CurrentPageIndex = e.NewPageIndex;
      start_index = ItemsGrid.CurrentPageIndex * ItemsGrid.PageSize;
      BindGrid();
     
   }   void BindGrid() 
   {      ItemsGrid.DataSource=CreateDataSource();
      ItemsGrid.DataBind();
     
   }</script><body>   <form runat=server>      <h3>DataGrid Custom Paging Example</h3>      <asp:DataGrid id="ItemsGrid" runat="server"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           AllowPaging="true"
           AllowCustomPaging="true"
           AutoGenerateColumns="false"
           OnPageIndexChanged="Grid_Change">         <PagerStyle NextPageText="Forward"
                     PrevPageText="Back"
                     Position="Bottom"
                     PageButtonCount="5"
                     BackColor="#00aaaa">
         </PagerStyle>         <AlternatingItemStyle BackColor="yellow">
         </AlternatingItemStyle>         <HeaderStyle BackColor="#00aaaa">
         </HeaderStyle>         <Columns>
      
            <asp:BoundColumn HeaderText="Number" 
                 DataField="IntegerValue"/>            <asp:BoundColumn
                 HeaderText="Item" 
                 DataField="StringValue"/>            <asp:BoundColumn 
                 HeaderText="Price" 
                 DataField="CurrencyValue" 
                 DataFormatString="{0:c}">               <ItemStyle HorizontalAlign="right">
               </ItemStyle>
   
            </asp:BoundColumn>         </Columns>
      
      </asp:DataGrid>      <br/>      <asp:CheckBox id="CheckBox1" 
           Text = "Show page navigation"
           AutoPostBack="true"
           runat="server"/>   </form></body>
</html>
要求
平台: Windows 2000, Windows XP Professional, Windows .NET Server family