艾伟:Gridview自定义排序且显示上下箭头

简介: 实现功能:单击Gidview列名按该列升序或降序排列,且在排序列上显示向上来向下箭头示意图片        //设置Gridview的AllowSorting属性值为true,即允许排序        AllowSorting="True" OnSorting="gridview1...

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

        //设置Gridview的AllowSorting属性值为true,即允许排序
        AllowSorting="True" OnSorting="gridview1_Sorting" OnRowCreated="gridview1_RowCreated" >

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

        //添加Sorting和RowCreated事件
        OnSorting="gridview1_Sorting" OnRowCreated="gridview1_RowCreated" >

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

         //设置默认表达式和排序顺序,放到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

目录
相关文章
|
8月前
WPF-样式问题-ListBox或ListView中子项全填充去除边线问题
WPF-样式问题-ListBox或ListView中子项全填充去除边线问题
72 0
【Flutter】GridView 网格布局 ( GridView.count 构造函数 | crossAxisCount 参数指定每行元素个数 )
【Flutter】GridView 网格布局 ( GridView.count 构造函数 | crossAxisCount 参数指定每行元素个数 )
461 0
【Flutter】GridView 网格布局 ( GridView.count 构造函数 | crossAxisCount 参数指定每行元素个数 )
|
C#
在wpf datagrid中,想要根据一个条件来改变datagrid行的背景颜色
原文:在wpf datagrid中,想要根据一个条件来改变datagrid行的背景颜色 在wpf datagrid中,想要根据一个条件来改变datagrid行的背景颜色 例如根据学生的年龄来修改,年龄小...
2909 0
|
C#
WPF 循环显示列表
原文:WPF 循环显示列表 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SANYUNI/article/details/79423707 项目需要类似手机上设置时间的控件,可以一直滚动显示的内容连续的。
1456 0

热门文章

最新文章