GridView Manual Sorting

///
/// The GridView_OnSorting event which is fired when the header of the column to sort is clicked.
///

///
///
protected void gvItems_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
DataTable dt = GetAllItems();
DataView dv = new DataView(dt);
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
dv.Sort = e.SortExpression + " DESC";
}
else
{
GridViewSortDirection = SortDirection.Ascending;
dv.Sort = e.SortExpression + " ASC";
}
gvItems.DataSource = dv;
gvItems.DataBind();
}
catch (Exception ex)
{ }
}

///
/// The GridViewSortDirection is a simple property which returns the new sort direction for the GridView
/// control. Since, the header of the column triggers a postback that is why it saves the last sort
/// direction into the ViewState object. This means that if the column was sorted in ascending order
/// then the new direction has to be descending.
///


public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set
{
ViewState["sortDirection"] = value;
}
}

Comments

Popular posts from this blog

To Move items from one ListBox to another Listbox

Receive Json Web response in C#