Asp.net Gridview:捕获排序方向
我在updatepanel中有一个gridview,它启用了排序,并且有一个事件处理程序,如下所示:Asp.net Gridview:捕获排序方向,asp.net,sorting,gridview,updatepanel,sortdirection,Asp.net,Sorting,Gridview,Updatepanel,Sortdirection,我在updatepanel中有一个gridview,它启用了排序,并且有一个事件处理程序,如下所示: protected void MyGridSort(object sender, GridViewSortEventArgs e) { var TheDirection = (e.SortDirection).ToString(); var TheColumn = (e.SortExpression).ToString(); } 我在这些行后面放了一个断点。每次按下列标题时,我的变
protected void MyGridSort(object sender, GridViewSortEventArgs e)
{
var TheDirection = (e.SortDirection).ToString();
var TheColumn = (e.SortExpression).ToString();
}
我在这些行后面放了一个断点。每次按下列标题时,我的变量TheDirection总是以升序显示
为什么它不能从上升到下降再返回
谢谢。您可以在ViewState或会话中保持方向。如下所示(未经测试的代码):
我一直在阅读,当您手动向gridview提供数据源时,排序似乎中断了。不确定这是否是你的情况,但这对我来说很有效
string strSortExpression = e.SortExpression + " ASC";
if (Convert.ToString(ViewState["SortExpression"]) == strSortExpression)
{
strSortExpression = e.SortExpression + " DESC";
}
ViewState["SortExpression"] = strSortExpression;
//This is done by sorting the Default View of the underlying data and then re-binding this
//to the grid.
System.Data.DataTable myData = HttpContext.Current.Session["GridData"] as System.Data.DataTable;
if (myData != null)
{
myData.DefaultView.Sort = strSortExpression;
GridView1.DataSource = myData;
GridView1.DataBind();
}
希望它有帮助无需使用会话变量或视图状态变量,您可以将其直接存储在GridView的
SortDirection
和SortExpression
属性中。
string strSortExpression = e.SortExpression + " ASC";
if (Convert.ToString(ViewState["SortExpression"]) == strSortExpression)
{
strSortExpression = e.SortExpression + " DESC";
}
ViewState["SortExpression"] = strSortExpression;
//This is done by sorting the Default View of the underlying data and then re-binding this
//to the grid.
System.Data.DataTable myData = HttpContext.Current.Session["GridData"] as System.Data.DataTable;
if (myData != null)
{
myData.DefaultView.Sort = strSortExpression;
GridView1.DataSource = myData;
GridView1.DataBind();
}