任何人都可以告诉函数在c#asp.net中对gridview的列进行排序.
gridview的数据绑定来自使用linq创建的datacontext.我想单击列的标题来对数据进行排序.
谢谢!
要做到这一点,您需要做两件事.
手动处理网格中的Sorting事件并使用我编写的这个帮助器按SortExpression和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);
}
Run Code Online (Sandbox Code Playgroud)
db.Products.SortBy(e.SortExpression,e.SortDirection)
查看我的博客文章,了解如何执行此操作:
归档时间: |
|
查看次数: |
34467 次 |
最近记录: |