1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| protected void Sort_LIST(object sender, GridViewSortEventArgs e)
{
List<T> myGridResults = (List<T>)Session["SOURCE"];
SortDirection _dir = SortDirection.Ascending;
if (ViewState["SORTING"] != null)
{
KeyValuePair<string, string> _old = (KeyValuePair<string, string>)ViewState["SORTING"];
if ((e.SortDirection.ToString() == _old.Key) && (e.SortExpression == _old.Value))
{
if (e.SortDirection == SortDirection.Ascending)
_dir = SortDirection.Descending;
else
_dir = SortDirection.Ascending;
}
}
if (myGridResults != null)
{
var param = Expression.Parameter(typeof(T), e.SortExpression);
var sortExpression = Expression.Lambda<Func<T, object>>(Expression.Convert(Expression.Property(param, e.SortExpression), typeof(object)), param);
if (_dir == SortDirection.Ascending)
{
GRIDVIEW1.DataSource = myGridResults.AsQueryable<T>().OrderBy(sortExpression);
ViewState["SORTING"] = new KeyValuePair<string, string>(e.SortDirection.ToString(), e.SortExpression.ToString());
}
else
{
GRIDVIEW1.DataSource = myGridResults.AsQueryable<T>().OrderByDescending(sortExpression);
ViewState["SORTING"] = null;
};
GRIDVIEW1.DataBind();
}
} |