//Sort User Table
private void SortGridView(string sortExpression, string direction)
{
DataTable dataTable = BindGridView(Session["useremail"].ToString()).Tables[0];
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = sortExpression + direction;
UserTable.DataSource = dataView;
UserTable.DataBind();
}
}
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, " ASC");
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, " DESC");
}
}
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}
当我编辑用户并更新编辑或进行一些搜索,并清除搜索页面加载并且排序丢失时,
private DataSet BindGridView(string email)
{
.......
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
.....
BindGridView(Session["useremail"].ToString());
}
每次加载页面或进行一些回发时,排序都会丢失,如何保留排序。
页面加载
if (PermissionList.Any(item => item.Equals("Edit user")))
{
if (!IsPostBack)
{
BindGridView(Session["useremail"].ToString());
}
}
答案 0 :(得分:0)
我遇到了类似的问题,我接近这个问题的方法是在会话中保存排序值,然后在页面加载丢失后重置主题。
答案 1 :(得分:0)
每当您对gridview执行新排序时,将排序表达式存储在隐藏的标签或字段中,并在您重新加载/绑定gridview时,使用已保存的排序表达式对表进行重新排序。
的.aspx
<asp:Label id="lblHidSortExp" runat="server" visible="false"></asp:Label>
.aspx.cs
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = lblHidSortExp.Text;
if(sortExpression == e.SortExpression)
sortExpression += " DESC";
else
sortExpression == e.SortExpression;
//not sure if this is exactly how you get your datatable, but you get the idea
DataView myView = new DataView(BindGridView(Session["useremail"].ToString()).Tables[0]);
myView.Sort = sortExpression;
marksGridView.DataSource = myView;
marksGridView.DataBind();
//save sort state
lblHidSortExp.Text = sortExpression;
}
所以在你的更新功能中说,使用你保存的排序exp
protected void btnUpdate_Click(object sender, EventArgs e)
{
.....//do update in db
//reload your table in dataview
DataView myView = new DataView(/*load table*/);
//do sort
myView.Sort = lblHidSortExp.Text;
//bind gridview
marksGridView.DataSource = myView;
marksGridView.DataBind();
}