2011-05-10 35 views
1

當我試圖解決一個GridView,系統將返回此錯誤消息:「System.StackOverflowException」排序一個GridView

GridView的排序類型 「System.StackOverflowException」未處理的異常發生在系統.Web.dll

這是代碼,「Melder」是要排序的列的名稱。

gvOutlookMeldingen.Sort("Melder", SortDirection.Ascending); 
+0

什麼類型都存儲在「Melder」列? – 2011-05-10 07:07:58

+0

對象的類型爲字符串 – SamekaTV 2011-05-10 07:15:38

+0

你是什麼數據源?你能完全寫出你的排序方法的代碼嗎? – ibram 2011-05-10 07:28:56

回答

2

把你的DataTable在ViewState中,當你綁定第一次

gridView1.DataBind(); 
ViewState["dtbl"] = YourDataTable 

,然後做像...

protected void ComponentGridView_Sorting(object sender, GridViewSortEventArgs e) 
{ 
DataTable dataTable = ViewState["dtbl"] as DataTable; 

if (dataTable != null) 
{ 
    DataView dataView = new DataView(dataTable); 
    dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection); 

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

private string ConvertSortDirection(SortDirection sortDirection) 
{ 
    string newSortDirection = String.Empty; 
switch (sortDirection) 
{ 
    case SortDirection.Ascending: 
    newSortDirection = "ASC"; 
    break; 

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

    return newSortDirection; 
} 

看看這裏也對物體的MSDN文章http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx

+0

「DataView」的+1。 – user12345613 2012-10-03 14:08:45

4

你可能會調用Sort()gvOutlookMeldingen_Sorting,這將再次調用gvOutlookMeldingen_SortingSort(),從而產生一個循環。

Sorting事件中,你需要調用更改數據源並再次進行查詢的功能。或者如果它被自動綁定,你不需要做任何事情。

資源