我和Listview控件有类似的问题。 我是这样解决的。
首先,我正在使用的代码从这个职位由Marc Gravell Dynamic LINQ OrderBy on IEnumerable<T>
在我的ListView的“OnSorting”事件添加以下代码。
protected void lv_Sorting(object sender, ListViewSortEventArgs e)
{
e.Cancel = true;
ViewState["OrderBy"] = e.SortExpression;
lvList.DataBind();
}
我加了一个相当标准的方式来捕捉sortdirection列表此
public SortDirection sortDirection
{
get
{
if (ViewState["sortdirection"] == null)
{
ViewState["sortdirection"] = SortDirection.Ascending;
return SortDirection.Ascending;
}
else if ((SortDirection)ViewState["sortdirection"] == SortDirection.Ascending)
{
ViewState["sortdirection"] = SortDirection.Descending;
return SortDirection.Descending;
}
else
{
ViewState["sortdirection"] = SortDirection.Ascending;
return SortDirection.Ascending;
}
}
set
{
ViewState["sortdirection"] = value;
}
}
在我的ListView中Selectmethod看起来像这样(使用马克扩展方法)
public IQueryable<SomeObject> GetObjects([ViewState("OrderBy")]String OrderBy = null)
{
var list = GETSOMEOBJECTS();
if (OrderBy != null)
{
switch (sortDirection)
{
case SortDirection.Ascending:
list = list.OrderByDescending(OrderBy);
break;
case SortDirection.Descending:
list = list.OrderBy(OrderBy);
break;
default:
list = list.OrderByDescending(OrderBy);
break;
}
}
return list;
}
我没有尝试过用GridView,但我相当肯定它的工作原理是一样的。
编辑 这里是LINQ扩展类的例子,应该工作
public static class LinqExtensions
{
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenByDescending");
}
static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
{
string[] props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach (string prop in props)
{
// use reflection (not ComponentModel) to mirror LINQ
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
object result = typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] { source, lambda });
return (IOrderedQueryable<T>)result;
}
}
只需添加一个使用“whatevernamespaceyouused”页面,你应该是好去。
你能粘贴您从控制器发送数据的代码。 – 2013-05-14 18:31:13
我建议看看这里,你可能需要添加一个'asp:LinqDataSource'标签:http://forums.asp.net/t/1213261.aspx/1 – 2013-05-14 18:33:23
Ismet这不是ASP.NET MVC。它是ASP.Net Webform 4.5,我试图使用ASP.NET 4.5提供的新模型绑定功能。 – dnguyen77 2013-05-14 18:48:05