我有一个网格绑定到一个linqed SP,因此:
Session["results"] = db.spGetCaseByNumberOrSurname(txtCaseNum.Text.Trim(), null).ToList();
gvResults.DataSource = Session["results"];
关于排序,我希望能够做到这一点.
protected void gvResults_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
List<spGetCaseByNumberOrSurnameResult> data = Session["results"] as List<spGetCaseByNumberOrSurnameResult>;
if (sd == SortDirection.Ascending)
{
sd = SortDirection.Descending;
gvResults.DataSource = data.OrderBy(d => d.GetType().GetProperty(sortExpression));
}
else
{
sd = SortDirection.Ascending;
gvResults.DataSource = data.OrderByDescending(d => d.GetType().GetProperty(sortExpression));
}
gvResults.DataBind();
}
可悲的是,它根本不进行任何排序.
“数据源不支持服务器端数据分页.”
有任何想法吗?
解决方法:
d.GetType().GetProperty(sortExpression)返回一个PropertyInfo对象.您需要的是这样的属性值:
d.GetType().GetProperty(sortExpression).GetValue(d, null);