private void OnGridViewSort(object sender, GridViewSortEventArgs e) { List<Product> prl = m_Provider.GetAllProducts(); if (prl != null) { var param = Expression.Parameter(typeof(Product), e.SortExpression); var sortExpression = Expression.Lambda<Func<Product, object>>(Expression.Convert(Expression.Property(param, e.SortExpression), typeof(object)), param); SortDir = (ViewState["SortDirection"] == null) ? SortDir = SortDirection.Ascending : (SortDirection)ViewState["SortDirection"]; if (!string.IsNullOrEmpty(e.SortExpression)) { if (e.SortExpression.Equals(this.SortExpression)) { SortDir = (SortDir == SortDirection.Ascending) ? SortDirection.Descending : SortDirection.Ascending; ViewState["SortDirection"] = SortDir; } this.SortExpression = e.SortExpression; } if (SortDir == SortDirection.Ascending) { m_GridView.DataSource = prl.AsQueryable<Product>().OrderBy(sortExpression); } else { m_GridView.DataSource = prl.AsQueryable<Product>().OrderByDescending(sortExpression); } m_GridView.DataBind(); } }