sorting a gridview bound to a linq SP
I have a grid bound to a linqed SP thus:
Session["results"] = db.spGetCaseByNumberOrSurname(txtCaseNum.Text.Trim(), null).ToList();
gvResults.DataSource = Session["results"];
on the sorting of it, i would like to be able to do this..
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(sortExp开发者_Python百科ression));
}
gvResults.DataBind();
}
sadly that doesnt do any sorting at all..it in fact errors with "The data source does not support server-side data paging."
any ideas?
d.GetType().GetProperty(sortExpression)
returns a PropertyInfo object. What you need is the value of the property like this:
d.GetType().GetProperty(sortExpression).GetValue(d, null);
精彩评论