在asp.net C#中的GridView的排序列 [英] sort columns of gridview in asp.net c#
问题描述
谁能告诉函数在C#中asp.net的GridView的列进行排序。
Can anyone tell the function to sort the columns of a gridview in c# asp.net.
数据绑定到GridView控件是创建的DataContext使用LINQ。我想单击列对数据进行排序的标题。
The databound to gridview is from datacontext created using linq. I wanted to click the header of the column to sort the data.
谢谢!
推荐答案
有两件事情你需要做的就是这个权利。
There are 2 things you need to do to get this right.
- 保持整理状态视图状态(SortDirection和SORTEX pression)
- 您生成正确的LINQ前pression基于当前的排序状态。
手动处理的排序在电网事件,并使用这个辅助我写了由SORTEX pression和SortDirection进行排序:
Manually handle the Sorting event in the grid and use this helper I wrote to sort by SortExpression and SortDirection:
public static IQueryable<T> SortBy<T>(IQueryable<T> source, string sortExpression, SortDirection direction) {
if (source == null) {
throw new ArgumentNullException("source");
}
string methodName = "OrderBy";
if (direction == SortDirection.Descending) {
methodName += "Descending";
}
var paramExp = Expression.Parameter(typeof(T), String.Empty);
var propExp = Expression.PropertyOrField(paramExp, sortExpression);
// p => p.sortExpression
var sortLambda = Expression.Lambda(propExp, paramExp);
var methodCallExp = Expression.Call(
typeof(Queryable),
methodName,
new[] { typeof(T), propExp.Type },
source.Expression,
Expression.Quote(sortLambda)
);
return (IQueryable<T>)source.Provider.CreateQuery(methodCallExp);
}
db.Products.SortBy(e.SortEx pression,e.SortDirection)
db.Products.SortBy(e.SortExpression, e.SortDirection)
查看<一个href=\"http://weblogs.asp.net/davidfowler/archive/2008/10/21/datacontrols-101-part-2-why-you-should-love-datasource-controls.aspx\">my如何做到这一点的博客文章:
这篇关于在asp.net C#中的GridView的排序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!