How to enable sorting on a GridView using an IEnumerable list as datasource
For the last months I’ve been working together with my colleague Raimond Brookman on new software for our software factory named Endeavour. The software is asp.net related. Today I ran into a problem with Microsofts GridView control. I was using an ObjectDataSource control that can provide a list of domain objects of type IEnumerable. This was all working fine until I wanted to enable sorting. I found out that built in sorting capability of the GridView control is not supported when using an object of type IEnumerable as a datasource. So I converted the IEnumerable list to a DataTable. But this class doesn’t support Nullable types and automatically converts my enum values to integers..:( That’s not what I wanted/needed.
Then I started thinking whether it could be possible to have this basic sorting done by using Linq! Well, it certainly is possible. Enumerable.OrderBy<> is your friend when it comes to sorting of a List of domain objects that need to be shown in a GridView. You can use Linq like this for example:
List<Order> orders = new List<Order>();
//<some code to fill the list… >
orders.OrderBy<Order, string>(o => o.OrderDate);
This code example above will sort the orders list ascending using the OrderDate property. It’s also possible to do a multi sort (sort on two properties) using ThenBy<> after the OrderBy<> method, like this:
List<Order> orders = new List<Order>();
//<some code to fill the list… >
orders.OrderBy<Order, string>(o => o.OrderDescription).ThenBy<Order, DateTime?>(o => o.OrderDate);
There are also OrderByDescending or ThenByDescending equivalents of the methods…
Now, in my piece of software that I’m building I don’t know upfront with what type of domain object I’m dealing with. So I cannot use the above statements (or something like that) in my code. Runtime I’ll have a list with domain objects, knowlegde of the type of the domain object and I’ll get the sort expression passed from the GridView control. Wouldn’t it be nice to be able to dynamically create a linq query with a lambda expression as in the above example…. It’s nice and also possible J! Here’s some code that will do exactly that. The method in the example below gets a list of objects, a sort expression (multi sort expression) and the type of the domain object passed as parameters. It builds the correct expression tree and executes it on the list. And voila, there’s a list of type IOrderedEnumerable that the GridView control can use to bind against. This is the first version of the code, I didn't test it thoroughly yet so there migth be some bugs in there. May be it also needs some refactoring, but it is just provided as an example to show how one could do this.
private IEnumerable SortSelectResult(IEnumerable selectResult, string sortExpression, Type searchResultType)
{
string[] expressions = sortExpression.Split(',');
bool isFirstLamdaExpression = true;
MethodCallExpression finalCallExpression = null;
///walk through the parts of the sortExpression (e.g. "Description ASC, OrderDate DESC")
foreach (string expression in expressions)
{
//Cut the SortDirection part loose from the column name
string[] expressionParts = expression.Split(' ');
//Determine sort direction
SortDirection sortDirection = DetermineSortDirection(expressionParts);
//Get the propertyInfo for this property
PropertyInfo propInfo = searchResultType.GetProperty(expressionParts[_ColummNamePartIndex]);
Type propertyType = propInfo.PropertyType;
//Build a lambda expression like this:
//pagedOrders.OrderBy<Order, string>(c => c.OrderDescription).ThenBy<Order, DateTime>(c => c.OrderDate);
//to enable sorting a enumerable list.
ParameterExpression param = Expression.Parameter(searchResultType, propInfo.Name);
Expression selector = Expression.Property(param, propInfo);
LambdaExpression lambdaExpression = Expression.Lambda(selector, param);
if (isFirstLamdaExpression)
{
string methodName = "OrderBy";
if (sortDirection == SortDirection.Descending)
{
methodName += "Descending";
}
MethodCallExpression orderByCall =
Expression.Call(typeof(Enumerable), methodName, new Type[] { searchResultType, propertyType },
Expression.Constant(selectResult), lambdaExpression);
finalCallExpression = orderByCall;
}
else
{
string methodName = "ThenBy";
if (sortDirection == SortDirection.Descending)
{
methodName += "Descending";
}
MethodCallExpression thenByCall =
MethodCallExpression.Call(finalCallExpression, methodName,
new Type[] { searchResultType, propertyType },
Expression.Constant(selectResult), lambdaExpression);
finalCallExpression = thenByCall;
}
}
if (finalCallExpression != null)
{
selectResult = (IEnumerable)selectResult.AsQueryable().Provider.Execute(finalCallExpression);
}
return (IEnumerable)selectResult;
}
private SortDirection DetermineSortDirection(string[] expressionParts)
{
if (expressionParts.Length <= 1 ||
string.IsNullOrEmpty(expressionParts[_SortDirectionPartIndex]) ||
expressionParts[_SortDirectionPartIndex].ToUpperInvariant() == "ASC")
{
return SortDirection.Ascending;
}
else if (expressionParts[_SortDirectionPartIndex].ToUpperInvariant() == "DESC")
{
return SortDirection.Descending;
}
else
{
return SortDirection.Ascending;
}
}
The OrderBy(Descending) / ThenBy(Descending) methods have also an overload where you can provide a comparer class, so that you can do some advanced stuff when needed. May be I'll need it also but I don't now yet ; ) Also I must say that the GridView control doesn't support multi sort out of the box, but it's not that hard to implement that.
Hope this helps!