Asp.Net GridView點擊標題排序方法

來源:互聯網
上載者:User
今天需要用到在GridView中點擊標題排序,琢磨了半天,終於成功了。在看代碼之前需要說明一點:
      GridView本身有一個Sort()函數:public virtual void Sort ( string sortExpression,    SortDirection sortDirection)使用上相當方便。但在實際使用中,常用的是點擊每一列的標題來排序,點擊標題會觸發:protected void GridView_Sorting(object sender, GridViewSortEventArgs e)事件。從GridViewSortEventArgs中可以拿到SortDirection和SortExpression。但是GridView.Sort()本身也會觸發GridView_Sorting(),如果在GridView_Sorting()中調用GridView.Sort()會無限的迴圈,直到堆疊溢位。這裡使用DataView.Sort屬性來排序,(Sort方法會觸發Sorting 事件),再重新綁定到GridView上。

代碼如下:

1、在源碼中給GridView中加入AllowSorting屬性    //1、在源碼的GridView中加入AllowSorting="true"    
    <asp:GridView ID="gvData" runat="server"  AllowSorting="true">
    </asp:GridView>

2、    ClbComunication.clsComunication類中定義了一個屬性,用於暫存從數據庫得到的資料:
    註:定義此屬性是因為頁面顯示後,剛才得的資料表(mdatatable)的資料會被清空。所以將得到的資料暫存於類中,便於調用。

        /// <summary>
        /// 存放資料DataView屬性的變量
        /// </summary>
        private static DataView dvValue = new DataView();
        /// <summary>
        /// 存放資料DataView屬性
        /// </summary>
        public static DataView dv
        {
            get { return dvValue; }
            set { dvValue = value; }
        }

3、將資料放入類的DataView屬性中儲存

        //得到資料(BOFactory.bofactory.GetOracleDataTable(mSQL_Selection)為從數據庫得到資料)
        DataTable mdatatable = BOFactory.bofactory.GetOracleDataTable(mSQL_Selection);
        //將需要排序的資料放入DataView中(主要用到此句)
        ClbComunication.clsComunication.dv = new DataView(mdatatable);
        //資料與介面上的GridView綁定
        gvData.DataBind();

4、訂閱GridView的Sorting事件

    //GridView的Sorting事件
    gvData.Sorting += new GridViewSortEventHandler(gvData_Sorting);

5、編輯GridView的Sorting事件方法,其中用到的GridViewSortDirection屬性的定義如"6"所示

    /// <summary>
    /// GridView的Sorting事件方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void gvData_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression.ToUpper();
        if (GridViewSortDirection ==SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            //排序並重新綁定
            bindData(sortExpression, "DESC");
        }
        else if (GridViewSortDirection == SortDirection.Descending)
        {
            GridViewSortDirection = SortDirection.Ascending;
            //排序並重新綁定
            bindData(sortExpression, "ASC");
        }
    }

6、定義排序方向屬性,此屬性用viewstate儲存每次排序的方向:

    /// <summary>
    /// 排序方向屬性
    /// </summary>
    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;
            return (SortDirection)ViewState["sortDirection"];
        }
        set
        {
            ViewState["sortDirection"] = value;
        }
    }

7、排序並綁定數據

    /// <summary>
    /// 排序並綁定數據
    /// </summary>
    /// <param name="sortExpression"></param>
    /// <param name="sortDirection"></param>
    protected void bindData(string sortExpression, string sortDirection)
    {
        ClbComunication.clsComunication.dv.Sort = sortExpression;
        if (sortDirection != String.Empty)
        {
            ClbComunication.clsComunication.dv.Sort = sortExpression+" " + sortDirection;
        }
        gvData.DataSource = ClbComunication.clsComunication.dv;
        gvData.DataBind();
    }

其它參考:

http://www.cnblogs.com/jackyrong/archive/2006/05/26/409788.html

http://stlh.blogspot.com/2008/02/gridview-sort.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.