How to perform sorting in agridview?

May 24 2014 5:31 AM
How to perform sorting in a grid view i am using this code but it is sorting the data in a single page and when we click on next page through paging the page is going blank and no data will be shown and again we click on paging same data will be reflecting on the first paging.
 
public SortDirection dir
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
BindGrid();
{
string SortDir = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
SortDir = "Desc";
}
else
{
dir = SortDirection.Ascending;
SortDir = "Asc";
}
DataView sortedView = new DataView(dt);
sortedView.Sort = e.SortExpression + " " + SortDir;
GridView1.DataSource = sortedView;
GridView1.DataBind();
}
}
 

Answers (3)