2016-08-19 77 views
0

我有一個GridView即我填寫使用sqldatasource我使用頁眉沒有問題排序在所有通過啓用AllowSorting="true"允許的GridView排序ASP C#

然後我過濾此GridView使用不同的控制器,我運行過濾功能下面使用DataTable

var mySqlConnection = //mySqlConnection 
    SqlCommand cmd = new SqlCommand(); 
    SqlDataAdapter da = new SqlDataAdapter(); 
    DataTable dt = new DataTable(); 

     cmd = new SqlCommand("SPRlist_GetSPRCombine", mySqlConnection); 
     cmd.Parameters.AddWithValue("@SPRKaimrcNo", sprkaimrcno); 
     cmd.Parameters.AddWithValue("@SPRNo", sprno); 
     cmd.Parameters.AddWithValue("@DateOfRequest", dateofrequest); 
     cmd.Parameters.AddWithValue("@RequesterBadge", requesterbadge); 
     cmd.Parameters.AddWithValue("@DeptID", department); 
     cmd.Parameters.AddWithValue("@SPRStatus", sprstatus); 
     mySqlConnection.Open(); 
     cmd.CommandType = CommandType.StoredProcedure; 
     da.SelectCommand = cmd; 
     da.Fill(dt); 

我打電話通過控制器變量的函數,這讓我回確切的結果:

GridViewSPRlist.DataSourceID = ""; 
    //Data Table Function Passing Controllers 
    GridViewSPRlist.DataBind(); 

的問題是:如果過濾GridView我不能對它進行排序,我也得到了以下錯誤:

The data source does not support sorting. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NotSupportedException: The data source does not support sorting.

如何啓用排序的DataTable的數據源?

+0

的可能的複製[排序的GridView綁定到數據表(http://stackoverflow.com/questions/3240252/sort-gridview-bound-to-datatable) –

+0

重複與http://stackoverflow.com/questions/10776372/allow-sorting-by-column-gridview –

+0

@TonyDong我已經檢查過它,並沒有爲我工作! –

回答

0

將代碼後面的DataTable自身排序並將其綁定到GridView。也許這是解決您的問題?

dt.DefaultView.Sort = "sortExpression DESC"; 
//use DefaultView.ToTable() if you want to use the sorted datatable later on in a viewstate or session etc. 
//dt.DefaultView.ToTable(); 

GridViewSPRlist.DataSource = dt; 
GridViewSPRlist.DataBind(); 
0

我在找到所有解決方案後找到了解決方案。 我刪除了sqldatasource負載和相同DataTable傳遞null函數的自變量中檢索整個數據集

功能進行排序(不要忘了把它在你的GridView OnSorting)取代了它:

protected void GridViewSPRlist_Sorting(object sender, GridViewSortEventArgs e) 
{ 
    DataTable dt = Session["TaskTable"] as DataTable; 

    if (dt != null) 
    { 
     //Sort the data. 
     dt.DefaultView.Sort = e.SortExpression + " " + (GetSortDirection(e.SortExpression)); 
     GridViewSPRlist.DataSource = Session["TaskTable"]; 

     GridViewSPRlist.DataBind(); 
    } 
} 

private string GetSortDirection(string column) 
{ 

    // By default, set the sort direction to ascending. 
    string sortDirection = "ASC"; 

    // 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 = "DESC"; 
      } 
     } 
    } 

    // Save new values in ViewState. 
    ViewState["SortDirection"] = sortDirection; 
    ViewState["SortExpression"] = column; 

    return sortDirection; 
} 

DataTable功能:

public DataTable GetSPRCombine(//Your Param) 
{ 
    var mySqlConnection = //Your Connection String 
    SqlCommand cmd = new SqlCommand(); 
    SqlDataAdapter da = new SqlDataAdapter(); 
    DataTable dt = new DataTable(); 
     cmd = new SqlCommand("SPRlist_GetSPRCombine", mySqlConnection); 
     //Your command parameters 
     mySqlConnection.Open(); 
     cmd.CommandType = CommandType.StoredProcedure; 
     da.SelectCommand = cmd; 
     da.Fill(dt); 
     Session["TaskTable"] = dt; 
    }