'System.StackOverflowException' when sorting a GridView

扶醉桌前 提交于 2019-12-08 04:37:57

问题


When I try to sort a GridView, the system returns this error-message:

gridview sort An unhandled exception of type 'System.StackOverflowException' occurred in System.Web.dll

This is the code and "Melder" is the name of the column to sort.

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

回答1:


You are probably calling Sort() inside gvOutlookMeldingen_Sorting, which will call gvOutlookMeldingen_Sorting and Sort() again, thus generating a loop.

On the Sorting event you need to call functions that alter the data source and perform the query again. Or if it's automatically bound, you don't need to do anything.

Resources

  • Sorting documentation



回答2:


Put your Datatable in Viewstate when you bind first time

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

and then do like...

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;
 }

Take a look here also on MSDN article http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx



来源:https://stackoverflow.com/questions/5946376/system-stackoverflowexception-when-sorting-a-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!