title: How to enable Paging and Sorting on ASP.NET 4.0 GridView programmatically?
body: <p>I am using ASP.NET 4.0 with C# (Visual Web Developer 2010 Express).</p>
  
  <p>I have successfully managed to implement a simple GridView bound to a stored procedure data source using declarative ASP.NET code as shown here:</p>
  
  <pre><code>&lt;asp:GridView 
      ID="grdTrades" 
      runat="server" 
      DataKeyNames="tradeId" 
      EnablePersistedSelection="true"
      SelectedRowStyle-BackColor="Yellow" 
      AllowPaging="true" 
      AllowSorting="true"
      PageSize = "20" 
      AutoGenerateColumns="false" 
      DataSourceID="sdsTrades" 
      &gt;
      &lt;Columns&gt;
          &lt;asp:CommandField ShowSelectButton="true" ButtonType="Link" SelectText="Select" /&gt;
          &lt;asp:BoundField DataField="tradeId" HeaderText="TradeId"  ReadOnly="True" SortExpression="tradeId" /&gt;
          &lt; ... more columns ... &gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  
  &lt;asp:SqlDataSource ID="sdsTrades" runat="server" 
      ConnectionString="&lt;%$ ConnectionStrings:TradesDB %&gt;" 
      ProviderName="&lt;%$ ConnectionStrings:Trades.ProviderName %&gt;"  
      SelectCommand="usp_GetTrades" SelectCommandType="StoredProcedure"&gt;      
  &lt;/asp:SqlDataSource&gt;
  </code></pre>
  
  <p>It works great including paging and sorting. I want to remove the SqlDataSource and use code-behind (I'm trying to put database access code in one place). So far I have this in my code-behind:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if (!this.IsPostBack)
      {
          grdTrades.SelectedIndex = 0;
          DBUtil DB = new DBUtil();
          grdTrades.DataSource = DB.GetTrades();
          grdTrades.DataKeyNames = new string[] { "tradeId" };
          grdTrades.DataBind();            
      }
  }
  
  // this is needed otherwise I get "The GridView 'grdTrades' fired event PageIndexChanging which wasn't handled."
  void grdTrades_PageIndexChanging(Object sender, GridViewPageEventArgs e)
  {
      grdTrades.PageIndex = e.NewPageIndex;
      grdTrades.DataBind();
  }    
  </code></pre>
  
  <p>My declarative code now looks like:</p>
  
  <pre><code>&lt;asp:GridView 
      ID="grdTrades" 
      runat="server" 
      EnablePersistedSelection="true"            
      SelectedRowStyle-BackColor="Yellow" 
      AllowPaging="true" 
      AllowSorting="true"
      PageSize = "20" 
      AutoGenerateColumns="false" 
  
      OnPageIndexChanging="grdTrades_PageIndexChanging"
      &gt;
      &lt;Columns&gt;
          &lt;asp:CommandField ShowSelectButton="true" ButtonType="Link" SelectText="Select" /&gt;
          &lt;asp:BoundField DataField="tradeId" HeaderText="TradeId"  ReadOnly="True" SortExpression="tradeId" /&gt;
          &lt; ... more columns ... &gt;           
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>The problem is when I click on a page number the page becomes blank. I would also like to implement sorting but would like to get the paging working first. Please help.</p>
  
  <p>Thanks</p>