I found a peice of code the other day in regards to sorting a gridview from a List<T> that might be helpful to alot of people. The cool thing is you do not need the SqlDataSource in order to use the builtin Sorting Capabilities. Below is the code:
protected void SortableNews_Sorting(object sender, GridViewSortEventArgs e)
{
if (GridViewSortDirection == SortDirection.Ascending)
{
((GridView)sender).DataSource = NewsCollection.OrderBy(mo => mo.GetType().GetProperty(e.SortExpression).GetValue(mo, null));
}
else
{
((GridView)sender).DataSource = NewsCollection.OrderByDescending(mo => mo.GetType().GetProperty(e.SortExpression).GetValue(mo, null));
}
GridViewSortDirection = GridViewSortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
((GridView)sender).DataBind();
}
private SortDirection GridViewSortDirection
{
get
{
if
(ViewState[
"sortDirection"
] ==
null
)
{
ViewState[
"sortDirection"
] =
SortDirection
.Ascending;
}
return
(
SortDirection
)ViewState[
"sortDirection"
];
}
set
{ ViewState[
"sortDirection"
] =
value
; }
}