Hello,
I have a gridview which I allow the users to page and sort. They both work fine, except if I sort a column, then click a different page, it doesn't keep the sort expression I originally chose (I choose Zip as the sort column, it sorts fine, then I click a different page and it's not sorted by Zip anymore). I'm thinking I need to call the SortGridView method after the paging, but I don't know how to get the current sortExpression.
Any suggestions would be greatly appreciated.
Thanks in advance.
I have a gridview which I allow the users to page and sort. They both work fine, except if I sort a column, then click a different page, it doesn't keep the sort expression I originally chose (I choose Zip as the sort column, it sorts fine, then I click a different page and it's not sorted by Zip anymore). I'm thinking I need to call the SortGridView method after the paging, but I don't know how to get the current sortExpression.
Any suggestions would be greatly appreciated.
Thanks in advance.
Code:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GetFormVars();
BindReport();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, " DESC");
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, " ASC");
}
}
private void SortGridView(string sortExpression, string direction)
{
// You can cache the DataTable for improving performance
GetFormVars();
BindReport();
//DataTable dt = GetData().Tables[0];
DataTable dt = GridView1.DataSource as DataTable;
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
GridView1.DataSource = dv;
GridView1.DataBind();
}