注册 | 登录 | 设为首页 | 加入收藏
您当前的位置:飞翔学院-IT中国 → 编程开发.Net/C# → 文章内容

gridview 排序问题

作者:佚名 来源:本站整理 发布时间:2008-1-5 12:06:56

我们在gridview中要对字段进行排序并不是一件容易的事情。

先将 AllowSorting="True"

然后将要排度的字段加上如 SortExpression="TotalMoney"

 <asp:BoundField DataField="TotalMoney" HeaderText="总金额"  SortExpression="TotalMoney" ItemStyle-HorizontalAlign="right" />

再添加一个 Sorting

//排序
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    ...{
        string SortAll = e.SortDirection.ToString().Replace("ending", "");

        if (Session["SortAll"] != null && Session["SortAll"].ToString() == "asc")
        ...{
            SortAll = "desc";
        }

        Session["SortAll"] = SortAll.Trim().ToLower();
        //Response.Write(SortAll);

        string SortTemp = e.SortExpression + " " + SortAll;
        Session["SortText"] = SortTemp;
        order_date(SortTemp);
        GridView1.DataBind();

       //Response.Write(e.SortDirection.ToString());
    }


    //排序
    protected void order_date(string SortText)
    ...{
        DataSet ds = ReadDate();
        GridView1.PageSize = 20;
        DataView dataView = new DataView(ds.Tables[0]);
        dataView.Sort = SortText;
        GridView1.DataSource = dataView;
    }

这里我们用 session 来保存排度的条件 因为每一次点击 e.SortDirection 永远都是 ASC 。另外要将排度的条件用Session保存是因为如果Gridview有分页的时候当点到第二页的时候排序就无效了。


  • 打印文档
  • 推荐好友
  • 返回顶部
  • 增大字体
  • 减少字体
关于本站 | 工作机会 | 合作网站 | 广告服务 | 市场合作| 联系我们 | 抽奖活动
版权所有: 武汉威俊科技有限公司 Copyright 2005-2007 www.ITCNW.COM All rights reserved