现在我已经实现了双向排序的功能,可是换页后排序就没有了,怎么实现在换页后排序功能保留if(!Page.IsPostBack)
{
ViewState["SortField"]="BranchID";
dtrBranch.Attributes["SortDirection"]="DESC";
cmd=new SqlCommand("Pager",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("@pageindex",1);
cmd.Parameters.Add("@pagesize",1);
cmd.Parameters.Add("@docount",true);
conn.Open();
pager.RecordCount=(int)cmd.ExecuteScalar();
conn.Close();
BindData();
}
void BindData()
{
cmd=new SqlCommand("Pager",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("@pageindex",pager.CurrentPageIndex);
cmd.Parameters.Add("@pagesize",pager.PageSize);
cmd.Parameters.Add("@docount",false);
conn.Open();
SqlDataAdapter myda=new SqlDataAdapter();
DataSet ds=new DataSet();
myda.SelectCommand=cmd;
myda.Fill(ds,"Btable");
DataView dv=ds.Tables["Btable"].DefaultView;
string SortDirection=dtrBranch.Attributes["SortDirection"];
dv.Sort=ViewState["SortField"].ToString()+" "+ SortDirection;
if(SortDirection=="ASC")
{

dtrBranch.Attributes["SortDirection"]="DESC";
}
else if(SortDirection=="DESC")
{
 
dtrBranch.Attributes["SortDirection"]="ASC";   
}
dtrBranch.DataSource=dv;
//dtrBranch.DataSource=cmd.ExecuteReader();
dtrBranch.DataBind();
conn.Close();
pager.CustomInfoText=" 页次:<font color=\"red\">"+pager.CurrentPageIndex.ToString()+"</font>/"+pager.PageCount.ToString()+"";
pager.CustomInfoText+="&nbsp;&nbsp;&nbsp;共<font color=\"red\" >"+pager.RecordCount.ToString()+"</font>条记录";
pager.CustomInfoText+="&nbsp;&nbsp;&nbsp;每页<font color=\"red\">"+pager.PageSize.ToString()+"</font>条"; }
public void dtrBranch_Sort(object src,DataGridSortCommandEventArgs e)
{

ViewState["SortField"]=e.SortExpression.ToString();
BindData();
}

解决方案 »

  1.   

    翻页后,给存储过程传的参数没了,所以建议用session保留一下,在传过去
      

  2.   

    要想实现翻页后继续排序,实现这样的效果:
    比如共 15笔记录,每页显示10条
    则排序时:第一页将前10条记录排序,翻第二页时后五条再单独排序.要注意以下几点: 
    1. 把数据集放到缓存中,例如: viewstate
    2. ViewState中不能存放DataView,那么只能存放: DataSet和上次是哪个e.expression并且存放此e.expression是升序还是降序示例如下:
    1.现有的排序事件是这样写的,这个是点击上面排序标题时用:
      private void grdProjTrace_SortCommand(object source, DataGridSortCommandEventArgs e)
    {
    this.grdProjTrace.CurrentPageIndex = 0;
    DataView dv = ((DataSet)this.ViewState["source"]).Tables[0].DefaultView;
    string strSort = "";
    string strOrder ="";//排序方式。0,降序,1升序
    if(ViewState["SortExpresstion"] != null)
    {
    strSort = ViewState["SortExpresstion"].ToString();
    strSort = strSort.Substring(0,strSort.Length -1);
    strOrder = ViewState["SortExpresstion"].ToString();
    strOrder = strOrder.Substring(strOrder.Length -1);
    }
    if(e.SortExpression == "CustomerName")
    {
    if(strSort != "CustomerName")
    {
    this.ViewState["SortExpresstion"] = ustomerName0";
    dv.Sort = "CustomerName DESC";
    }
    else
    {
    if(strOrder == "0")
    {
    this.ViewState["SortExpresstion"] = "CustomerName1";
    dv.Sort = "CustomerName ASC";
    }
    else
    {
    this.ViewState["SortExpresstion"] = "CustomerName0";
    dv.Sort = "CustomerName DESC";
    }
    }
    }
    if(e.SortExpression == "fullName")
    {
    if(strSort != "fullName")
    {
    this.ViewState["SortExpresstion"] = "fullName0";
    dv.Sort = "fullName DESC";
    }
    else
    {
    if(strOrder == "0")
    {
    this.ViewState["SortExpresstion"] = "fullName1";
    dv.Sort = "fullName ASC";
    }
    else
    {
    this.ViewState["SortExpresstion"] = "fullName0";
    dv.Sort = "fullName DESC";
    }
    }
    }
            this.grdProjTrace.DataSource = dv;
    this.grdProjTrace.DataBind();
    }2.下面这个方法是自己写的,翻页后调用。
    private void ChangePageDataBind()
    {
    DataView dv = ((DataSet)this.ViewState["source"]).Tables[0].DefaultView;
    string strSort = "";
    string strOrder ="";//排序方式。0,降序,1升序
    if(ViewState["SortExpresstion"] != null)
    {
    strSort = ViewState["SortExpresstion"].ToString();
    strSort = strSort.Substring(0,strSort.Length -1);
    strOrder = ViewState["SortExpresstion"].ToString();
    strOrder = strOrder.Substring(strOrder.Length -1);
    }
    if(this.ViewState["SortExpresstion"] != null)
    {
    if(strSort == "CustomerName")
    {
    if(strOrder == "1")
    {
    this.ViewState["SortExpresstion"] = "CustomerName1";
    dv.Sort = "CustomerName ASC";
    }
    else
    {
    this.ViewState["SortExpresstion"] = "CustomerName0";
    dv.Sort = "CustomerName DESC";
    }
    }
    }
    if(this.ViewState["SortExpresstion"] != null)
    {
    if(strSort == "fullName")
    {
    if(strOrder == "1")
    {
    this.ViewState["SortExpresstion"] = "fullName1";
    dv.Sort = "fullName ASC";
    }
    else
    {
    this.ViewState["SortExpresstion"] = "fullName0";
    dv.Sort = "fullName DESC";
    }
    }
    }
    this.grdProjTrace.DataSource = dv;
    this.grdProjTrace.DataBind();
    }上面两方法只要修改要排序的字段名,就可以直接调用了.
    1方法很简单使用,这里就不说了.
    2方法是这样用的:
    private void grdProjTrace_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
    try
    {
    try
    {
    this.grdProjTrace.CurrentPageIndex = e.NewPageIndex;
    }
    catch
    {
    this.grdProjTrace.CurrentPageIndex = 0;
    }
    this.ChangePageDataBind();
    }
    catch(System.Exception errWS)
    {
             //异常
    }
    }