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