使用GridView的Sorting事件,在分页的时候,点击第二页,之前单击表头呈现的排序效果又没有了。怎么样才能在分页的时候依然能保持之前的排序效果。

解决方案 »

  1.   

    分页方法需要自己写,不然分页就不能保持排序状态。
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (e.SortExpression != "")
            {
                if (GridView1.PageCount > 0)
                {
                    string SortDirection = "ASC";
                  SortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == SortDirection ? "DESC" : "ASC");
                    this.GridView1.Attributes["SortExpression"] = e.SortExpression;
                    this.GridView1.Attributes["SortDirection"] = SortDirection;
                    //重新绑定数据
                    DataBind();
                }
            }
        }
      

  2.   

    如果楼主是使用自带的分页,建议楼主使用
    SqlDataSource,或者如果分层的话,使用ObjectDataSource,
    只要设置一下GridView1.AllowSorting = true;即OK了!
      

  3.   

     //数据绑定
            public void DataBound(string _WhereStr)
            {
                //防止刷新
                if (this.isRefresh()) 
                { 
                    return;
                }
                if (string.IsNullOrEmpty(_WhereStr))
                {
                    _WhereStr = " 1=1";
                }
                string aidSelect = "SELECT * from emp,dep where emp.dep_id = dep.dep_id and emp_type = '1' and " + _WhereStr;
                DataTable fileTable = new DataTable();
                fileTable = DbAccess.GetDS(aidSelect).Tables[0];            if (fileTable.Rows.Count == 0)
                {
                    //Response.Write("<script>alert('没有符合此条件的记录!')</script>");
                    //DbUITool.JsMsg(this.Page, "没有符合此条件的记录!");
                    return;
                }
                else
                {
                    GridView1.DataSource = fileTable;
                    GridView1.DataBind();
                }
                this.lblCurrentPage.Text = string.Format("当前第{0}页/总共{1}页", this.GridView1.PageIndex + 1, this.GridView1.PageCount);
            }
            //GridView分页
            protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                GridView1.PageIndex = e.NewPageIndex;
                tiaojian = this.lbtiaojian.Text.Trim();
                DataBound(tiaojian);
            }   
      

  4.   

    你首先在pageload里面定义 一个ViewState["sort"]=""  DataSet ds_todayjzb = tTodayJzbBLL.queryTodayJzbByCondition(condition);
      if (ViewState["sort"] == "")
            {
                ds_todayjzb.Tables[0].DefaultView.Sort = " jzbh desc";
            }
            else
            {
                ds_todayjzb.Tables[0].DefaultView.Sort = ViewState["sort"].ToString();
            }
            gv_todayjzb.DataSource = ds_todayjzb.Tables[0].DefaultView;
            gv_todayjzb.DataBind();