GridView 排序

GridView 排序时只能按照一种方式排序 及e.SortDirection始终为"Ascending",下面是自己做的一点修改
保存视图状态:
/// <summary>
    /// 保存排序顺序
    /// </summary>
    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;
            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value; }
    }
GridView的排序事件:
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        try
        {
            DataView dv = 得到数据集;
            if (GridViewSortDirection == SortDirection.Ascending)
            {
                GridViewSortDirection = SortDirection.Descending;
                dv.Sort = sortExpression + " desc";               
            }
            else
            {
                GridViewSortDirection = SortDirection.Ascending;
                dv.Sort = sortExpression + " asc";               
            }
            GridView1.DataSource = dv;
            GridView1.DataBind();
        }
        catch
        {
           
        }
    }

posted on 2007-03-28 09:17  myer  阅读(566)  评论(1编辑  收藏  举报

导航