Export (0) Print
Expand All

DataGrid.AllowCustomPaging Property

Gets or sets a value that indicates whether custom paging is enabled.

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

public virtual bool AllowCustomPaging { get; set; }
/** @property */
public boolean get_AllowCustomPaging ()

/** @property */
public void set_AllowCustomPaging (boolean value)

public function get AllowCustomPaging () : boolean

public function set AllowCustomPaging (value : boolean)

Property Value

true if custom paging is enabled; otherwise, false. The default value is false.

Paging allows you to display the contents of the DataGrid control in page segments. The number of items on a page is determined by the PageSize property. If no value is specified for the PageSize property, the DataGrid will display 10 items on a page.

Normally, a data source that contains every row in the DataGrid control is loaded every time the DataGrid control moves to a different page. This can consume a lot of resources when the data source is very large. Custom paging allows you to load just the segment of data needed to display a single page.

To enable custom paging, set both the AllowPaging and AllowCustomPaging properties to true. Next, provide code to handle the PageIndexChanged event.

The typical logic for the PageIndexChanged event handler is to first set the CurrentPageIndex property to the index of the page you want to display.


The event handler receives a DataGridPageChangedEventArgs object as a parameter. You can use the NewPageIndex property of this parameter to determine the index of the page selected by the user from the page selection elements of the DataGrid control.

Next, create a data source that contains the data to display on a single page and then use the DataBind method to bind the data to the DataGrid control.


Because only a segment of the data is loaded, you must set the VirtualItemCount property to the total number of items in the DataGrid control. This allows the control to determine the total number of pages needed to display every item in the DataGrid control. This property is normally programmatically set once the total number of items in the DataGrid control is determined.

When paging is enabled with the AllowCustomPaging property is set to false, the DataGrid control assumes that the data source contains all the items to be displayed. The DataGrid control calculates the indexes of the items on the displayed page based on the page index, specified by the CurrentPageIndex property, and the number of items on a page, specified by the PageSize property.

When the AllowCustomPaging property is set to true, the DataGrid control assumes that the data source only contains the items as determined by the VirtualItemCount property. All items up to the number of items specified by the PageSize property are displayed.

The following code example demonstrates how to use the AllowCustomPaging property to enable custom paging.

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>


   <script runat="server">

      // Normally, an entire data source is loaded in the DataGrid control, 
      // which can take up a lot of resources. This example uses custom 
      // paging, which loads only the segment of data needed to fill a
      // single page. In order to query for the appropriate segment of
      // data, the index of the first item displayed on a page must be
      // tracked as the user navigates between pages.
      int startIndex = 0;

      ICollection CreateDataSource() 

         // Create sample data for the DataGrid control.
         DataTable dt = new DataTable();
         DataRow dr;

         // Define the columns of the table.
         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
         dt.Columns.Add(new DataColumn("DateTimeValue", typeof(string)));
         dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));

         // Populate the table with sample values. When using custom paging,
         // a query should only return enough data to fill a single page, 
         // beginning at the start index.
         for (int i = startIndex; i < (startIndex + MyDataGrid.PageSize); i++) 
             dr = dt.NewRow();

             dr[0] = i;
             dr[1] = "Item " + i.ToString();
             dr[2] = DateTime.Now.ToShortDateString();
             dr[3] = (i % 2 != 0) ? true : false;


         DataView dv = new DataView(dt);
         return dv;


      void Page_Load(Object sender, EventArgs e) 

         // Load sample data only once, when the page is first loaded.
         if (!IsPostBack) 

            // Set the virtual item count, which specifies the total number
            // items displayed in the DataGrid control when custom paging
            // is used.
            MyDataGrid.VirtualItemCount = 200;

            // Retrieve the segment of data to display on the page from the
            // data source and bind it to the DataGrid control.



      void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e) 

         // For the DataGrid control to navigate to the correct page when
         // paging is allowed, the CurrentPageIndex property must be updated
         // programmatically. This process is usually accomplished in the
         // event-handling method for the PageIndexChanged event.

         // Set CurrentPageIndex to the page the user clicked.
         MyDataGrid.CurrentPageIndex = e.NewPageIndex;

         // Calculate the index of the first item to display on the page 
         // using the current page index and the page size.
         startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;

         // Retrieve the segment of data to display on the page from the 
         // data source and bind it to the DataGrid control.


      void BindGrid() 

         MyDataGrid.DataSource = CreateDataSource();




   <form runat="server">
      <h3> DataGrid Custom Paging Example </h3>

      <asp:DataGrid id="MyDataGrid" 

         <HeaderStyle BackColor="Navy" 
                      Font-Bold="True" />

         <PagerStyle Mode="NumericPages" 
                     HorizontalAlign="Right" />




Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

© 2015 Microsoft