title: How to make asp:GridView sortable?
body: <p>i have an <code>asp:GridView</code> control, which i've set the <code>AllowSorting="True"</code> property on:</p>
  
  <pre><code>&lt;asp:GridView ID="gridUsers" runat="server" PageSize="100" ShowHeaderWhenEmpty="True"
     Width="100%" AllowSorting="True" onrowcreated="gridUsers_RowCreated" 
     onsorting="gridUsers_Sorting"&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>At design time the grid looks sortable:</p>
  
  <p><img src="https://i.stack.imgur.com/W52YB.png" alt="enter image description here"></p>
  
  <p>But at runtime only the middle column is sortable:</p>
  
  <p><img src="https://i.stack.imgur.com/McZxX.png" alt="enter image description here"></p>
  
  <p>How do i make an <code>asp:GridView</code> sortable in ASP.NET?</p>
  
  <hr>
  
  <p><strong>Note</strong>: The <code>asp:GridView</code> with <code>AllowSorting</code> <em>requires</em> a <code>Sorting</code> event handler to be present:</p>
  
  <pre><code>protected void gridUsers_Sorting(object sender, GridViewSortEventArgs e)
  {
     //asp:GridView will throw an exception if a Sorting event handler isn't present
  }
  </code></pre>
  
  <p><strong>Update</strong>: i realized what's special about the <strong>Description</strong> column. It's the only column whose display name is correct from the database <em>as-is</em>. The remaining columns <a href="https://stackoverflow.com/questions/2091457/how-to-hide-columns-in-an-asp-net-gridview-with-auto-generated-columns">i have to fix the display name to be presentable</a>:</p>
  
  <pre><code>protected void gridUsers_RowCreated(object sender, GridViewRowEventArgs e)
  {
     e.Row.Cells[0].Visible = false; //UserGUID
     e.Row.Cells[1].Text = "User name";
     e.Row.Cells[2].Text = "Full name";
     //3=Description
     e.Row.Cells[4].Text = "E-mail";
     e.Row.Cells[5].Text = "Active";
         e.Row.Cells[5].Visible = false;
     e.Row.Cells[6].Text = "Account type";
   }
  </code></pre>
  
  <p>Now i just have to figure out the tricky part; and make columns sortable.</p>