更新パネルにあるグリッドビューがあります。列ヘッダーをクリックすると並べ替えは正常に機能しますが、次のページに移動すると、並べ替えが失われます。以下のコードを提供します。この混乱から抜け出す方法について誰かがアイデアを持っている場合は、助けてください!
ShowGrid()関数はPage_Loadで呼び出されます。問題は、ページング関数でShowGrid()を呼び出すことにあると思います。
//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();
}
}