2014-08-27 20 views
0

即使查看了一些示例,我也無法得到正確的結果。對gridview控件進行排序 - 方向永不改變

我有了這個代碼,它高興地重新排序我的GridView在升序:

// gridViewSorting and ConvertSortDirectionToSql are both necessary to ensure the gridview can sort when their column headers are 
    // clicked. You must remember to add (AllowSorting="True" OnSorting="gridViewSorting") to the gridview tag on the ASP side 
    protected void gridViewSorting(object sender, GridViewSortEventArgs e) 
    { 
     DataTable dataTable = GVInactive.DataSource as DataTable; 

     if (dataTable != null) 
     { 
      DataView dataView = new DataView(dataTable); 
      string SQL = "[" + e.SortExpression + "] " + ConvertSortDirectionToSql(e.SortDirection); 
      dataView.Sort = SQL; 

      GVInactive.DataSource = dataView.ToTable(); 
      GVInactive.DataBind(); 

     } 
    } 

    private string ConvertSortDirectionToSql(SortDirection sortDirection) 
    { 
     string newSortDirection = String.Empty; 

     switch (sortDirection) 
     { 
      case SortDirection.Ascending: 
       newSortDirection = "DESC"; 
       break; 

      case SortDirection.Descending: 
       newSortDirection = "ASC"; 
       break; 
     } 

     return newSortDirection; 
    } 

但是,我第二次點擊它應該扭轉以前的排序順序的頭。它永遠不會。每次點擊列的標題時,它都會觸及case SortDirection.Ascending:行並設置newSortDirection =「DESC」。數據按降序排列,但當我再次單擊標題時,它會將SortDirection解析爲升序。

任何想法?

回答

1

我們使用ViewState變量來存儲最新的排序方向。當網格被排序時,我們將網格的排序標準和排序方向與存儲最後排序表達式的ViewState變量進行比較。如果列相同,則檢查上一個排序的方向並按相反方向排序。

例子:

private string SortCriteria 
    { 
     get 
     { 
      if (ViewState["sortCriteria"] == null) 
      { 
       ViewState["sortCriteria"] = ""; 
      } 

      return ViewState["sortCriteria"].ToString(); 
     } 
     set 
     { 
      ViewState["sortCriteria"] = value; 
     } 
    } 

    private string SortDirection 
    { 
     get 
     { 
      if (ViewState["sortDirection"] == null) 
      { 
       ViewState["sortDirection"] = ""; 
      } 

      return ViewState["sortDirection"].ToString(); 
     } 
     set 
     { 
      ViewState["sortDirection"] = value; 
     } 
    } 

    protected void gvData_Sorting(object sender, GridViewSortEventArgs e) 
    { 
     gvData.EditIndex = -1; 

     if (SortCriteria == e.SortExpression) 
     { 
      if (SortDirection == string.Empty || SortDirection == "DESC") { SortDirection = "ASC"; } 
      else { SortDirection = "DESC"; } 
     } 
     else 
     { 
      SortCriteria = e.SortExpression; 
      SortDirection = "ASC"; 
     } 

     BindGrid(); 
    } 

    private void BindGrid() 
    { 
     DataTable dt = new [However you get dataset from database]; 
     DataView dv = new DataView(dt); 
     dv.Sort = string.Format("{0} {1}", SortCriteria, SortDirection).Trim(); 

     gvData.DataSource = dv; 
     gvData.DataBind(); 
    } 
+0

它看起來像BindGrid()是一個函數。你能否發佈該功能?沒有它我只有一半的答案。另外,我是否需要任何特殊參考才能使其工作? – 2014-08-29 14:29:37

+0

好點。我現在已經使用了BindGrid方法,它顯示了SortCriteria和SortDirection屬性的用法。對不起,疏忽了。 – laughsloudly 2014-08-29 17:02:32

0

爲了記錄在案,我代替我這個問題使用的代碼,它的工作完美

// gridViewSorting and ConvertSortDirectionToSql are both necessary to ensure the gridview can sort when their column headers are 
    // clicked. You must remember to add (AllowSorting="True" OnSorting="gridViewSorting") to the gridview tag on the ASP side 
    protected void gridViewSorting(object sender, GridViewSortEventArgs e) 
    { 
     DataTable dataTable = GVInactive.DataSource as DataTable; 
     string sortExpression = e.SortExpression; 
     string direction = string.Empty; 

     if (dataTable != null) 
     { 
      DataView dataView = new DataView(dataTable); 

      if (SortDirection == SortDirection.Ascending) 
      { 
       SortDirection = SortDirection.Descending; 
       direction = " DESC"; 
      } 
      else 
      { 
       SortDirection = SortDirection.Ascending; 
       direction = " ASC"; 
      } 

      DataTable table = GVInactive.DataSource as DataTable; 
      table.DefaultView.Sort = sortExpression + direction; 

      GVInactive.DataSource = table; 
      GVInactive.DataBind(); 

     } 
    } 

    public SortDirection SortDirection 
    { 
     get 
     { 
      if (ViewState["SortDirection"] == null) 
      { 
       ViewState["SortDirection"] = SortDirection.Ascending; 
      } 
      return (SortDirection)ViewState["SortDirection"]; 
     } 
     set 
     { 
      ViewState["SortDirection"] = value; 
     } 
    } 

我也許可以刪除幾行(不知道我什至需要DataView),但它完美的工作。只要記住這些作品在GridView標記添加到ASP方面:

AllowSorting = 「真」 OnSorting = 「gridViewSorting」

,然後改變你的GridView的名稱在適當情況。

相關問題