title: ASP.NET DataGrid and custom paging
body: <p>I'm trying to implement a DataGrid in ASP.NET, and want to achieve custom paging so that I don't have to provide all the data in one go. I've spent several hours researching on the internet, but haven't found anything useful.</p>
  
  <p>When I view the page I see the first set of results in the grid, with the previous link disabled.  When I click next however, I once again see the first page of the grid with the previous link disabled. When debugging the code I ascertained that the MyGrid_PageIndexChanged() event handler is never called.</p>
  
  <p>I've included my simplified code below. I've changed variable names and omited methods to focus on the datagrid paging issue.</p>
  
  <p>In the ASPX file:</p>
  
  <pre><code>&lt;asp:DataGrid ID="myGrid" runat="server" GridLines="None" UseAccessibleHeader="true" AutoGenerateColumns="false" AllowPaging="true" AllowCustomPaging="true" PageIndexChanged="MyGrid_PageIndexChanged"&gt;
  &lt;PagerStyle Mode="NextPrev" NextPageText="Next &gt;" PrevPageText="&lt; Previous" /&gt;
  
  &lt;Columns&gt;
  &lt;asp:BoundColumn HeaderText="Title" DataField="Name" /&gt;
  &lt;asp:BoundColumn HeaderText="Date" DataField="Date" /&gt;
  &lt;/Columns&gt;
  &lt;/asp:DataGrid&gt;
  </code></pre>
  
  <p>And in the CS file:</p>
  
  <pre><code>    protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
                              myGrid.PageSize = 20;
                              myGrid.VirtualItemCount = GetNumItems();
              BindMyGrid();
          }
      }
  
      protected void MyGrid_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
      {
          myGrid.CurrentPageIndex = e.NewPageIndex;
          BindMyGrid();
      }
  
      private int GetNumItems()
      {
          return 500;
      }
  
      private void BindMyGrid()
      {
              Data[] array = GetDataFromInternetSomehow();
              this.myGrid.DataSource = array;
              this.myGrid.DataBind();
      }
  
      private class Data
      {
          public string Date { get; set; }
          public string Name { get; set; }
      }
  </code></pre>
  
  <p>Any thoughts on this would be much appreciated.</p>