AllowSorting="True" OnSorting="gridvie " /> 精品国精品国产尤物美女,免费污视频在线,heyzo高清中文字幕在线

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

Gridview自定義排序且顯示上下箭頭

        實(shí)現(xiàn)功能:?jiǎn)螕鬐idview列名按該列升序或降序排列,且在排序列上顯示向上來(lái)向下箭頭示意圖片

        //設(shè)置Gridview的AllowSorting屬性值為true,即允許排序
        AllowSorting="True" OnSorting="gridview1_Sorting" OnRowCreated="gridview1_RowCreated" >

        //為要排序的列加上SortExpression屬性,其值為綁定的字段,如:
        SortExpression="ID">

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

       后臺(tái)代碼,創(chuàng)建如下方法:

         //設(shè)置默認(rèn)表達(dá)式和排序順序,放到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數(shù)據(jù)的函數(shù)
        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()函數(shù)中的SQL語(yǔ)句

   //將SortDirection轉(zhuǎn)化為SQL語(yǔ)句中的ASC和DESC  

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

   在原SQL語(yǔ)句后加上order by語(yǔ)句: "order by "+ ViewState["SortExpression"]+" "+sortStr

NET技術(shù)Gridview自定義排序且顯示上下箭頭,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 留坝县| 武穴市| 同江市| 长顺县| 寻乌县| 南江县| 武威市| 德令哈市| 循化| 长丰县| 永吉县| 剑川县| 七台河市| 齐河县| 长兴县| 古蔺县| 东明县| 丽江市| 桐城市| 揭西县| 布拖县| 承德市| 双辽市| 漳平市| 文登市| 株洲县| 扶沟县| 星子县| 彩票| 景宁| 六枝特区| 凤翔县| 慈利县| 巴彦淖尔市| 林西县| 延庆县| 鹰潭市| 毕节市| 黑山县| 沂水县| 珠海市|