网格视图排序为特定列 [英] Grid view sorting for particular column
问题描述
我想code约在Asp.net C#网格视图为特定的列进行排序。
当ASC我有点一列,当去第二列,并尝试对它进行排序将其作为DESC。我要为每列一个单独的ASC DESC顺序。
I want code about sorting for particular column in GRID VIEW in Asp.net C#. When I sort one column in ASC and when go for a second Column and try to sort it takes it as DESC. I want a separate ASC DESC order for each column.
推荐答案
有关分类
请排序状态视图状态(SortDirection和SORTEX pression)
您生成基于当前的排序状态正确的LINQ前pression。
手动处理在网格中的排序活动,并使用这个辅助我写了由SORTEX pression和SortDirection进行排序:
Keep the sorting state is viewstate(SortDirection and SortExpression) You generate the correct linq expression based on the current sorting state. Manually handle the Sorting event in the grid and use this helper I wrote to sort by SortExpression and SortDirection:
公共静态的IQueryable SortBy(IQueryable的来源,串SORTEX pression,SortDirection方向){
如果(来源== NULL){
抛出新的ArgumentNullException(源);
}
public static IQueryable SortBy(IQueryable 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);
}
这篇关于网格视图排序为特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!