20160308001 GridView的Sorting排序

参考地址: http://www.cnblogs.com/yinluhui0229/archive/2011/08/01/2124169.html

功能介绍:单击gridview的某一列列头,可以对该列进行排序。
实现方法:
1.设置AllowSorting="True"
2.在现实的字段中添加SortExpression属性,值为改字段的DataField
  如:
<asp:BoundField DataField="BILLCODE" HeaderText="单据号" SortExpression="BILLCODE" />
<asp:BoundField DataField="CODE" HeaderText="货号" SortExpression="CODE" />
3.添加GridView的Sorting方法
  现在Page_Load()方法中定义一个当前GV的排序顺序,并给出一个默认值:
  ViewState["sortdirection"] = "ASC";
  Sorting()方法中的代码如下:
        protected void grvPro_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (publicDS != null)
            {
                DataView dv = new DataView(publicDS);//publicDS为绑定GV的数据源
                if (ViewState["sortdirection"].ToString() == "ASC")
                {
                    dv.Sort = e.SortExpression + " DESC";
                    ViewState["sortdirection"] = "DESC";
                }
                else
                {
                    dv.Sort = e.SortExpression + " ASC";
                    ViewState["sortdirection"] = "ASC";
                }
                grvPro.DataSource = dv;
                grvPro.DataBind();
            }
        }

posted @ 2016-03-08 15:00  追逐苦痛  阅读(246)  评论(0编辑  收藏  举报