2009-09-15 137 views
1

有一個簡單的 「淨」 的方式來做到分層分類.net GridView的分層(多列)排序?

表...

A|B|C 
----- 
1|2|5 
2|8|4 
2|4|3 
3|7|2 
4|4|1 

點擊一個,則B會得到你..

A|B|C 
----- 
1|2|5 
2|4|3 
2|8|4 
3|7|2 
4|4|1 

的變化因爲我在(A)等語境中排序(B)。

很明顯,這可以在數據源中進行管理,但想知道是否有人有一個優雅和可擴展的解決方案..謝謝。

回答

2

如果你問在尋呼一起做這件事的背後,有沒有簡單的,可擴展的解決方案。事實上,這是商業應用程序web開發的聖盃。例如,請參閱StackOverflow問題Dynamic Sorting within SQL Stored Procedures,它涉及同樣的事情。畢竟,如果我們在數據庫服務器上進行了動態排序,那麼我們只需編寫用於管理用戶排序選擇的機制。

你真的只對多列排序三個選項:

  • 做在客戶端,讓你的數據容器做繁重(當你使用具有這種功能的數據容器內置,如System.Data.DataView)。

  • 編寫自己的算法並在綁定前自行排序數據。

  • 通過上面鏈接中討論的解決方案之一在數據庫服務器上執行此操作。

這兩種客戶端解決方案都不具備真正的可擴展性,因爲它們涉及在您只需要一個子集時提取並提供所有數據。

0

ASPX頁面

<asp:GridView id="MyGridView" runat="server" AllowSorting="true" OnSorting="MyGridView_OnSorting"> 
    <asp:BoundField DataField="ColumnA" SortExpression="A" /> 
    <asp:BoundField DataField="ColumnB" SortExpression="B" /> 
    <asp:BoundField DataField="ColumnC" SortExpression="C" /> 
</asp:GridView> 

代碼

protected void MyGridView_OnSorting(object sender, GridViewSortEventArgs e) 
{ 
    List<MyEntity> data = MyBLL.GetDataSource(); 

    data.Sort(delegate(MyEntity x, MyEntity y) { 
     switch(e.SortExpression) 
     { 
      case "ColumnA": 
       return String.Compare(x.ColumnA, y.ColumnA); 
       break; 
      case "ColumnB": 
       return String.Compare(x.ColumnB, y.ColumnB); 
       break; 
      case "ColumnC": 
       return String.Compare(x.ColumnC, y.ColumnC); 
       break; 
     } 
    } 
    ); 

    MyGridView.DataSource = data; 
    MyGridView.DataBind(); 
} 
+0

除非我要去瘋了,這隻會排在一列,對吧? – 2009-09-15 20:35:48

+0

是...至少15個字符 – 2009-09-15 20:38:01

+0

啊..是的..不會工作然後..需要在多列上排序.. – madcolor 2009-09-15 20:39:25

2

爲了得到一個ASP.NET的GridView正確排序時,多列在綁定列的排序規範上市以後,你需要給電網的OnSorting事件綁定到這個函數:

protected void gridViewSorting(object sender, GridViewSortEventArgs e) 
{ 
    // 
    // This odd function permits GridView objects to sort on multiple columns 
    // Without this function, a GridView object does not sort correctly when multiple 
    // columns are named in its sort specification. 
    // 

    if (!(sender is GridView)) 
    { 
     return; 
    } 
    if (!e.SortExpression.Contains(',')) 
    { 
     return; 
    } 

    GridView gv = sender as GridView; 

    // 
    // Find the column that is to become the basis of the sort 
    // 
    foreach (DataControlField dc in gv.Columns) 
    { 
     String fieldSortExprClean = dc.SortExpression.Replace(" DESC", ""); 
     fieldSortExprClean = fieldSortExprClean.Replace(" ASC", ""); 

     String eventSortExprClean = e.SortExpression.Replace(" DESC", ""); 
     eventSortExprClean = eventSortExprClean.Replace(" ASC", ""); 

     if (fieldSortExprClean == eventSortExprClean) 
     { 
      if (e.SortDirection == SortDirection.Ascending) 
      { 
       dc.SortExpression = fieldSortExprClean.Replace(",", " ASC,"); 
       e.SortExpression = fieldSortExprClean.Replace(",", " ASC,"); 
      } 
      else 
      { 
       dc.SortExpression = fieldSortExprClean.Replace(",", " DESC,"); 
       e.SortExpression = fieldSortExprClean.Replace(",", " DESC,"); 
      } 
     } 
    } 
} 
+0

謝謝...這對我有用:) – 2013-04-12 12:12:48