Handle GridView.OnSorting() and Create a Sorting Expression using LinQ

GridView control from ASP.NET 2.0 like this one is widely used:

<asp:GridViewrunat="server"ID="gridPersons"AutoGenerateColumns="false"AllowSorting="true"OnSorting="gridPersons_Sorting"><Columns><asp:BoundFieldHeaderText="First name"DataField="FirstName"SortExpression="FirstName"/><asp:BoundFieldHeaderText="Last name"DataField="LastName"SortExpression="LastName"/></Columns></asp:GridView>
protected void gridPersons_Sorting(object sender, GridViewSortEventArgs e)
{
    string sortExpression = e.SortExpression;
}


 

However, it has one limitation: the sorting event argument GridViewSortEventArg is based on string values from markup, which means it cannot be used in programmatic sorting using LINQ.  This article will describe how to convert a string argument to a sorting expression to be used in LINQ.

Read the rest of the tutorial here: http://www.codeproject.com/Articles/246361/Handle-GridView-OnSorting-using-LINQ

Leave a Reply

Your email address will not be published. Required fields are marked *