I have a gridview that is in an updatepanel. The sort works fine when you click the column headers but if I go to the next page, the sort is lost. I will provide the code below and if ANYONE out there has an idea on how to get out of this mess, please help!
The ShowGrid() function is called in the Page_Load. I am guessing the problem lies with recalling ShowGrid() in the paging function.
//Show Grid based on argument type
protected void ShowGrid(string arg)
{
//dataset to hold email addresses
DataSet ds = new DataSet();
try
{
//open connection with new connection string
conn.Open();
String selectString = String.Format("SELECT * FROM hr_OnlineJobApp"
+ " WHERE adminCategory='" + adminCategory + "'"
+ "AND departmentApplyingFor LIKE '" + dept + "' ORDER BY {0} {1}", ViewState["sortExp"], ViewState["sortOrder"]);
SqlCommand cmd = new SqlCommand(selectString, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds);
//bind to gridview
GridView1.DataSource = ds;
GridView1.DataBind();
GridView1.PageIndex = Convert.ToInt32(ViewState["pageIndex"]);
}
finally
{
if (conn != null)
conn.Close();
}
}
/**********************************************
* Paging functionality
* *******************************************/
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//reshow Grid with current type
adminCategory = GridView1.DataKeys[0]["adminCategory"].ToString();
ViewState["pageIndex"] = e.NewPageIndex.ToString();
ShowGrid(adminCategory);
}
/**********************************************
* Sorting functionality
* *******************************************/
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExp = e.SortExpression;
ViewState["sortExp"] = sortExp;
string sortDir = (string)ViewState["sortOrder"];
//Changes the sortDir
if (ViewState["sortOrder"].ToString() == "desc")
{
ViewState["sortOrder"] = "asc";
}
else
{
ViewState["sortOrder"] = "desc";
}
try
{
conn.Open();
string sql = "SELECT * FROM hr_OnlineJobApp"
+ " WHERE adminCategory='" + adminCategory + "'"
+ "AND departmentApplyingFor LIKE '" + dept + "' ORDER BY " + sortExp + " " + sortDir;
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(sql, conn);
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
GridView1.DataSource = myDataSet;
GridView1.DataBind();
}
finally
{
conn.Close();
}
}
You are setting the
PageIndexto 0 inShowgrid:Therefore you’re overwriting the new value from
PageIndexChangingSo you just don’t need to set the PageIndex in ShowGrid to fix the issue.
Another possible reason for the issue:
Maybe you’re databinding the GridView on every postback from
Page_Load. You should do that only at the first time. Use thePage.IsPostBackproperty:Last but not least: you are sorting only if the user clicks on the header in
gridView_Sorting. You should apply theORDER BYalso inShowGridor use only one method to databind your GridView. But you’re always ordering bydate descthere.You should not use a DataView to apply the sort, instead you should use the
ORDER BYin the sql at the first place.