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

这篇博客介绍了如何在GridView中实现自定义排序功能,当用户点击列头时,数据将按照该列升序或降序排列。同时,排序列上会显示相应的上下箭头图片,以指示当前的排序状态。作者提供了后台代码实现排序逻辑,包括设置默认排序表达式、获取排序列索引、添加排序图片以及处理Sorting和RowCreated事件。在SQL查询中,还展示了如何根据排序方向添加'ORDER BY'子句。

实现功能:单击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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值