开发者

'System.StackOverflowException' when sorting a GridView

When I want to sort my GridView manually I get this error: gridview sort An unhandled exception of type 'Syst开发者_JAVA百科em.StackOverflowException' occurred in System.Web.dll

This is the line of code I run. "Melder" is the correct name of a column which I want to sort on.

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

Thank you in advance.


You have to handle the PageIndexChanging and Sorting events"

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.DataSource = SortDataTable(GetYourDataSource(), true);
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataBind();
}
private string GridViewSortDirection
{
    get { return ViewState["SortDirection"] as string ?? "ASC"; }
    set { ViewState["SortDirection"] = value; }
}
private string GridViewSortExpression
{
    get { return ViewState["SortExpression"] as string ?? string.Empty; }
    set { ViewState["SortExpression"] = value; }
}
private string ToggleSortDirection()
{
    switch (GridViewSortDirection)
    {
        case "ASC":
            GridViewSortDirection = "DESC";
            break;
        case "DESC":
            GridViewSortDirection = "ASC";
            break;
   }
   return GridViewSortDirection;
}
protected DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)
{
    if (dataTable != null)
    {
        DataView dataView = new DataView(dataTable);
        if (GridViewSortExpression != string.Empty)
        {
            if (isPageIndexChanging)
            {
                dataView.Sort = string.Format("{0} {1}",  GridViewSortExpression,GridViewSortDirection);
            }
            else
            {
                dataView.Sort = string.Format("{0} {1}",  GridViewSortExpression,ToggleSortDirection());
            }
       }
        return dataView;
    }
    else
    {
        return new DataView();
   }
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    GridViewSortExpression = e.SortExpression;
    int pageIndex = GridView1.PageIndex;
    GridView1.DataSource = SortDataTable(GetYourDataSource(), false);
    GridView1.PageIndex = pageIndex;
    GridView1.DataBind();
}


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


I stumbled over your question here while I was having the same problem.

After doing some research and reading the MSDN help on the Sort method I would have wanted to ask if you were calling this method from within the GridView's Sorting event (which I did)?

This would be the wrong approach, because the Sort method calls the Sorting event again, which would result in an endless loop.

I don't know why, but Sort() is supposed to work only outside of the Sorting event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜