如何在gridview中对特定页面进行排序?

时间:2014-04-11 05:58:19

标签: c# asp.net sorting gridview paging

以下是代码的一部分:

private void BindData()
{  
       .................
      sda.Fill(ds);
      if (ds.Tables[0].Rows.Count > 0)
      {
        DataView DV = ds.Tables[0].DefaultView;

        if (Session["sortBy"] != null) 
        {
            DV.Sort = Session["sortBy"].ToString();
        }
        GridView1.DataSource = DV;
        GridView1.DataBind();
    }
    else
    {
        GridView1.DataSource = null;
        GridView1.DataBind();
    }

}

排序代码:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{

Session["sortBy"] = e.SortExpression + " " + GetSortDirection(e.SortExpression);
BindData();

}

private string GetSortDirection(string column)
{


string sortDirection = "ASC";

string sortExpression = ViewState["SortExpression"] as string;

if (sortExpression != null)
{

    if (sortExpression == column)
    {
        string lastDirection = ViewState["SortDirection"] as string;
        if ((lastDirection != null) && (lastDirection == "ASC"))
        {
            sortDirection = "DESC";
        }
    }
}

ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;

return sortDirection;
}

分页部分工作正常,当我对页面进行排序时,数据源中的所有数据都会被排序,但我想只对gridview中的特定页面进行排序。如何实现?

0 个答案:

没有答案