title: Making GridView sortable when manually asigning DataSource
body: <p>I have a gridview that I would like to have sortable.  When I define it like this:</p>
  
  
  
  <pre class="lang-html prettyprint-override"><code>&lt;asp:GridView ID="GridView1" runat="server" CssClass="gridview" DataSourceID="GridDataSource" AutoGenerateColumns="False" AllowSorting="True" &gt;
  </code></pre>
  
  
  
  <p>The sorting works just fine.  ie. when I click the column header the grid sorts by that column.</p>
  
  <p>But when I define it like this:
  </p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;asp:GridView ID="GridView1" runat="server" CssClass="gridview"  AutoGenerateColumns="False" AllowSorting="True" &gt;
  </code></pre>
  
  <p>and in codebehind:</p>
  
  <pre><code>GridView1.DataSource = GridDataSource;
  GridView1.DataBind();
  </code></pre>
  
  
  
  <p>Using the latter method the sorting does not work. The column headers are links but the grid does not order itself.</p>
  
  <p>I'm setting the DataSource after the page load so the user can chose from some filters (dropdown lists) to restrict the records returned.</p>
  
  <p>Can someone tell me how to set the datasource in codebehind and get sorting functioning?  Alternatively a good way to stop the grid from databinding on initial page load.</p>