Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thank you for your answers on the sorting. I turned to LINQ to help sort dynamically. Since the grid knows whether to sort ASC or DESC, and which field, I used a LINQ Expression. The Expression performed the sorting, and then I simply bound those results to my gridview.</p> <p>I suspect the jQuery method would be faster, and wouldn't require a full postback. </p> <pre><code>using System.Linq.Expressions; public SortDirection GridViewSortDirection { get { if (ViewState["sortDirection"] == null) ViewState["sortDirection"] = SortDirection.Ascending; return (SortDirection)ViewState["sortDirection"]; } set { ViewState["sortDirection"] = value; } } protected void gridView_Sorting(object sender, GridViewSortEventArgs e) { //re-run the query, use linq to sort the objects based on the arg. //perform a search using the constraints given //you could have this saved in Session, rather than requerying your datastore List&lt;T&gt; myGridResults = PerfomSearch(); if (myGridResults != null) { var param = Expression.Parameter(typeof(T), e.SortExpression); var sortExpression = Expression.Lambda&lt;Func&lt;T, object&gt;&gt;(Expression.Convert(Expression.Property(param, e.SortExpression), typeof(object)), param); if (GridViewSortDirection == SortDirection.Ascending) { myGridView.DataSource = myGridResults.AsQueryable&lt;T&gt;().OrderBy(sortExpression); GridViewSortDirection = SortDirection.Descending; } else { myGridView.DataSource = myGridResults.AsQueryable&lt;T&gt;().OrderByDescending(sortExpression); GridViewSortDirection = SortDirection.Ascending; }; myGridView.DataBind(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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