zoukankan      html  css  js  c++  java
  • [转]Gridview自定义排序且显示上下箭头

    实现功能:单击Gidview列名按该列升序或降序排列,且在排序列上显示向上来向下箭头示意图片

            //设置Gridview的AllowSorting属性值为true,即允许排序
            <asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="False" EmptyDataText="没有相关记录!" AllowSorting="True" OnSorting="gridview1_Sorting" OnRowCreated="gridview1_RowCreated" >
            </asp:GridView>

            //为要排序的列加上SortExpression属性,其值为绑定的字段,如:
            <asp:BoundField DataField="ID" HeaderText="序号" SortExpression="ID">

            //添加Sorting和RowCreated事件
            <asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="False" EmptyDataText="没有相关记录!" AllowSorting="True" OnSorting="gridview1_Sorting" OnRowCreated="gridview1_RowCreated" >
            </asp:GridView>

           后台代码,创建如下方法:

             //设置默认表达式和排序顺序,放到page_load事件中
            public void SetSorting()
            {
                ViewState["SortExpression"] = "ID";
                ViewState["SortDirection"] = SortDirection.Descending;

            }

            //获取排序列索引
            private int GetSortColumnIndex()
            {
                foreach (DataControlField field in gridview1.Columns)
                {
                    if (field.SortExpression == ViewState["SortExpression"].ToString().Trim())
                        return gridview1.Columns.IndexOf(field);
                }
                return -1;
            }

            //添加排序图片
            private void AddSortImage(int columnIndex, GridViewRow headerRow)
            {
                Image sortImage = new Image();
                if ((SortDirection)ViewState["SortDirection"] == SortDirection.Ascending)
                {
                    sortImage.ImageUrl ="向上箭头图片的路径";
                }
                else
                {
                    sortImage.ImageUrl = "向下箭头图片的路径";
                }

                headerRow.Cells[columnIndex].Controls.Add(sortImage);
            }
           
         
    //Gridview的Sorting事件 
          protected void gridview1_Sorting(object sender, GridViewSortEventArgs e)
         {
            if (ViewState["SortExpression"].ToString().Trim() == e.SortExpression)
            {
                if ((SortDirection)ViewState["SortDirection"] == SortDirection.Ascending)
                    ViewState["SortDirection"] = SortDirection.Descending;
                else
                    ViewState["SortDirection"] = SortDirection.Ascending;
            }
            else
            {
                ViewState["SortExpression"] = e.SortExpression;
                ViewState["SortDirection"] = SortDirection.Descending;
            }
            
            //你绑定Gridview数据的函数
            GvBind();
        }
        //Gridview的RowCreated事件 
        protected void gridview1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                int sortColumnIndex = GetSortColumnIndex();

                if (sortColumnIndex != -1)
                    AddSortImage(sortColumnIndex, e.Row);
            }
        }
       

       最后,还要修改GvBind()函数中的SQL语句

       //将SortDirection转化为SQL语句中的ASC和DESC  

       string sortStr = ((SortDirection)ViewState["SortDirection"] == SortDirection.Descending ? "DESC" : "");

       在原SQL语句后加上order by语句: "order by "+ ViewState["SortExpression"]+" "+sortStr

  • 相关阅读:
    谁能撼动C语言的霸主地位?是Go?是Rust?还是...
    从入职到快速晋升,程序员能够从底层逆袭,究竟是什么原因?
    我敢说,这8个Linux基础命令,学了绝不吃亏!(强烈推荐)
    想从程序员到百万高管,你一定要避开这3个坑!(年轻人必读)
    注意!在Linux中删除特殊名称文件有这6种方式!(建议收藏)
    事实证明!这10个Linux系统操作指令,简直 “不讲武德” ,请大家耗子尾汁!
    最适合单片机编程的高级语言,除了C语言,别无选择!
    “Help”!如何学习C和C++才不茫然,才不是乱学?
    C语言丨不要阅读此文,除非你已掌握二叉树的这些操作
    团队编程项目作业2-团队编程项目开发环境搭建过程
  • 原文地址:https://www.cnblogs.com/zhangzt/p/1651296.html
Copyright © 2011-2022 走看看