【问题标题】:'System.StackOverflowException' when sorting a GridView对 GridView 进行排序时出现“System.StackOverflowException”
【发布时间】:2011-08-22 05:28:45
【问题描述】:

当我尝试对 GridView 进行排序时,系统返回此错误消息:

gridview sort 类型未处理的异常 System.Web.dll 中发生“System.StackOverflowException”

这是代码,“Melder”是要排序的列的名称。

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

【问题讨论】:

  • “Melder”列中存储了哪些类型的对象?
  • 对象的类型是字符串
  • 你的数据源是什么?你能完全写出你的排序方法的代码吗?
  • 我没有数据源,GridView 充满了来自 Exchange 服务的项目。
  • 天哪,检查他的个人资料.. 他问了两次同样的问题

标签: c# asp.net sorting gridview stack-overflow


【解决方案1】:

您可能在gvOutlookMeldingen_Sorting 中调用Sort(),这将再次调用gvOutlookMeldingen_SortingSort(),从而产生一个循环。

Sorting 事件中,您需要调用更改数据源的函数并再次执行查询。或者如果它是自动绑定的,你不需要做任何事情。

资源

【讨论】:

    【解决方案2】:

    第一次绑定时将数据表放入 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

    【讨论】:

      猜你喜欢
      • 2013-12-23
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 2016-10-01
      相关资源
      最近更新 更多