C# ';System.StackOverflowException';对GridView排序时

C# ';System.StackOverflowException';对GridView排序时,c#,asp.net,sorting,gridview,C#,Asp.net,Sorting,Gridview,当我想手动排序我的GridView时,我得到了以下错误:GridView排序System.Web.dll中发生了类型为“System.StackOverflowException”的未处理异常 这是我运行的代码行。“Melder”是我要排序的列的正确名称 gvOutlookMeldingen.Sort("Melder", SortDirection.Ascending); 提前感谢。您必须处理页面索引交换和排序事件” 您必须处理PageIndexchange和排序事件“ 第一次绑定时,将数据表

当我想手动排序我的GridView时,我得到了以下错误:GridView排序System.Web.dll中发生了类型为“System.StackOverflowException”的未处理异常

这是我运行的代码行。“Melder”是我要排序的列的正确名称

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

提前感谢。

您必须处理页面索引交换和排序事件”


您必须处理PageIndexchange和排序事件“


第一次绑定时,将数据表置于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文章

第一次绑定时将数据表置于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文章

当我遇到同样的问题时,我无意中发现了你的问题

在做了一些研究并阅读了关于排序方法的MSDN帮助之后,我想问一下您是否从GridView的排序事件中调用了这个方法(我是这么做的)

这将是错误的方法,因为Sort方法再次调用排序事件,这将导致无休止的循环


我不知道为什么,但是Sort()应该只在排序事件之外工作。

当我遇到同样的问题时,我在这里遇到了你的问题

在做了一些研究并阅读了关于排序方法的MSDN帮助之后,我想问一下您是否从GridView的排序事件中调用了这个方法(我是这么做的)

这将是错误的方法,因为Sort方法再次调用排序事件,这将导致无休止的循环


我不知道为什么,但是Sort()应该只在排序事件之外工作。

我使用devexpress aspgridview为我这样做,但我假设这不是一个选项…我使用devexpress aspgridview的可能副本为我这样做,但我假设这不是一个选项…我只需要排序而不是分页的可能副本。我看到您想要访问数据源。我清楚地说我不能这样做,因为我的GridView中没有数据源对不起!我猜是datasourceControl。我不使用它。@Tassist那么在GridView中如何显示记录呢?我从Exchange Server读取数据并将其放入数据表中。我不能为了对这个数据表进行排序而反复执行这个操作,因为我必须从Exchange再次访问它。这需要很多时间。就像Bharath刚才说的。我把它放到一个数据表中。然后将数据表填充到GridView1.DataSource。它不是一个数据源控件。我只需要排序,不需要分页。我看到您想要访问数据源。我清楚地说我不能这样做,因为我的GridView中没有数据源对不起!我猜是datasourceControl。我不使用它。@Tassist那么在GridView中如何显示记录呢?我从Exchange Server读取数据并将其放入数据表中。我不能为了对这个数据表进行排序而反复执行这个操作,因为我必须从Exchange再次访问它。这需要很多时间来完成。就像Bharath刚才说的。我把它放到一个DataTable中。然后将DataTable填充到GridView1.DataSource。它不是一个datasourceControl。我要做的就是更改网格控件的名称以匹配我的代码。这是一个非常好的答案。我要做的就是更改网格控件的名称以匹配我的代码。多好的回答啊。