2011-05-21 87 views
1

我有一個問題,一直在困擾我很長一段時間,我急需幫助,因爲我是一個.NET初學者。如何保持GridView的排序狀態? (升序和降序)

我正在使用GridView來顯示查詢結果。然而,我不是直接將其注入到實體/ LINQ數據源,而是手動編寫諸如加載,排序和分頁之類的事件。問題在於排序,我無法保持上升/下降狀態。我可以想到的一個可能的解決方案是通過緩存狀態,但是,我覺得還有另一種更整潔的方式。因此,你們可以向我暗示其他更適合的想法嗎?

非常感謝! 下面是我用於排序的代碼。顯然,e.SortDirection將永遠等於ascending無論我點擊了列的標題多少次。

switch (e.SortExpression) 
     { 
      case "Album": 
       if (e.SortDirection == SortDirection.Ascending) 
        _orderedResult = from doc in _result 
            orderby doc.DocumentAlbum.Name ascending 
            select doc; 
       else 
        _orderedResult = from doc in _result 
            orderby doc.DocumentAlbum.Name descending 
            select doc; 
       break; 
      case "Category": 
       if (e.SortDirection == SortDirection.Ascending) 
        _orderedResult = from doc in _result 
            orderby doc.DocumentCategory.Name ascending 
            select doc; 
       else 
        _orderedResult = from doc in _result 
            orderby doc.DocumentCategory.Name descending 
            select doc; 
       break; 
      case "Title": 
       if (e.SortDirection == SortDirection.Ascending) 
        _orderedResult = from doc in _result 
            orderby doc.Title ascending 
            select doc; 
       else 
        _orderedResult = from doc in _result 
            orderby doc.Title descending 
            select doc; 
       break; 
      case "Description": 
       if (e.SortDirection == SortDirection.Ascending) 
        _orderedResult = from doc in _result 
            orderby doc.Description ascending 
            select doc; 
       else 
        _orderedResult = from doc in _result 
            orderby doc.Description descending 
            select doc; 
       break; 
      case "DateCreated": 
       if (e.SortDirection == SortDirection.Ascending) 
        _orderedResult = from doc in _result 
            orderby doc.DateCreated ascending 
            select doc; 
       else 
        _orderedResult = from doc in _result 
            orderby doc.DateCreated descending 
            select doc; 
       break; 
      case "DateUpdated": 
       if (e.SortDirection == SortDirection.Ascending) 
        _orderedResult = from doc in _result 
            orderby doc.DateUpdated ascending 
            select doc; 
       else 
        _orderedResult = from doc in _result 
            orderby doc.DateUpdated descending 
            select doc; 
       break; 

     } 
+0

你在ASP這樣做。淨? – 2011-05-21 17:53:13

+0

你可以從這個線索得到想法http://stackoverflow.com/questions/5947780/how-to-convert-a-gridview-to-datatable-and-sort-the-datatable/5947912#5947912 – 2011-05-21 18:06:07

回答

1

其實我只是找到了答案。我使用ViewState函數來跟蹤狀態。 這是我使用的功能:

private SortDirection GetSortDirection(string column) 
    { 
     // By default, set the sort direction to ascending 
     SortDirection _sortDirection = SortDirection.Ascending; 

     // Retrieve the last column that was sorted 
     string _sortExpression = ViewState["SortExpression"] as string; 

     if (_sortExpression != null) 
     { 
      // Check if the same column is being sorted. 
      // Otherwise, the default value can be returned. 
      if (_sortExpression == column) 
      { 
       string _lastDirection = ViewState["SortDirection"] as string; 
       if ((_lastDirection != null) && (_lastDirection == "ASC")) 
       { 
        _sortDirection = SortDirection.Descending; 
       } 
      } 
     } 

     // Save new values in ViewState. 
     ViewState["SortDirection"] = _sortDirection.ToString(); 
     ViewState["SortExpression"] = column; 

     return _sortDirection; 
    } 
0
protected void gvOfflineSub_Sorting(object sender, GridViewSortEventArgs e) 
    { 
     IList<SellerDetails> Ilist = gvOfflineSub.DataSource as IList<SellerDetails>; 
     DataTable dt = ToDataTable<SellerDetails>(Ilist); 
     if (dt != null) 
     { 
      DataView dataView = new DataView(dt); 
      dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection); 

      gvOfflineSub.DataSource = dataView; 
      gvOfflineSub.DataBind(); 
     } 
    } 

    /// <summary> 
    /// Description:Following Method is for Sorting Gridview. 
    /// </summary> 
    /// <param name="sortDirection"></param> 
    /// <returns></returns> 
    private string ConvertSortDirectionToSql(SortDirection sortDirection) 
    { 
     string newSortDirection = String.Empty; 

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

      case SortDirection.Descending: 
       newSortDirection = "DESC"; 
       break; 
     } 
     return newSortDirection; 
    } 
0

如果您正在使用LINQ那麼它是非常簡單的排序 只是綁定電網這樣

var data = (from d in edc.TableName 
       select new 
       { 
        d.Address, 
        d.InsertedDate,       
        d.ID 
       }).OrderByDescending(d => d.ID); 
    if (data != null) 
    { 
     grdList.DataSource = data; 
     grdList.DataBind(); 
    } 
相關問題