Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to enable sorting (<strong><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.allowsorting.aspx" rel="nofollow noreferrer">AllowSorting</a></strong>) and handle the event <strong><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx" rel="nofollow noreferrer">OnSorting</a></strong>.</p> <p><strong><em>Note: The sample code uses C#, but the VB version should be similar.</em></strong></p> <p>Create your <strong>GridView</strong>:</p> <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AllowSorting="True" OnSorting="GridView1_Sorting"&gt; &lt;/asp:GridView&gt; </code></pre> <p>Handle <strong>OnSorting</strong>:</p> <pre><code>protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { GridView1.DataSource = GetObjects(e.SortDirection, e.SortExpression); GridView1.DataBind(); } </code></pre> <p><strong>GetObjects</strong> returns a sorted <code>List&lt;MyObject&gt;</code>. You have to create your own sorting logic here, one alternative could be using <a href="https://stackoverflow.com/questions/41244/dynamic-linq-orderby">Dynamic Linq</a>. If you choose that route, <strong>GetObjects</strong> could be defined like this: (there are better ways, but this is enough to show the theory)</p> <pre><code>private List&lt;MyObject&gt; GetObjects(SortDirection sd, string se) { // Have we generated data before? if (SimulatedDB == null) { // Create a sample DB SimulatedDB = new List&lt;MyObject&gt;(); var rnd = new Random(); for (int i = 0; i &lt; 20; i++) { var node = new MyObject(); node.Id = i; node.Name = String.Format("Name {0}", i); node.CreationDate = DateTime.Now.AddDays(rnd.Next(100)); node.Amount = (rnd.Next(1000) * rnd.NextDouble()); SimulatedDB.Add(node); } } // Return sorted list if (sd == SortDirection.Ascending) return SimulatedDB.AsQueryable&lt;MyObject&gt;().OrderBy&lt;MyObject&gt;(se).ToList(); else return SimulatedDB.AsQueryable&lt;MyObject&gt;().OrderByDescending&lt;MyObject&gt;(se).ToList(); } </code></pre> <p>Hope it helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload