Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a gridview and I'm trying to do Sorting on a Column. When I do that, it throws 'StackOverflow' Exception

What I have tried:

aspx Code:

<asp:GridView ID="grdItems" runat="server"  Width="100%" AllowPaging="True"
                CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" OnRowDataBound="grdItems_RowDataBound" AllowSorting="True" OnSorting="grdItems_Sorting">
                <FooterStyle BackColor="White" Font-Bold="True" ForeColor="#808080"/>
                <RowStyle BackColor="#EFF3FB" />
                <EditRowStyle BackColor="#2461BF" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <pagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" Font-Size="Small" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
                <Columns>

                    <asp:BoundField DataField="actionItemId" HeaderText="Item Id" SortExpression="actionItemId" >
                        <ItemStyle Font-Size="Small" VerticalAlign="Top" />
                        <HeaderStyle Font-Bold="True" Font-Size="Small" HorizontalAlign="Left" Width="65px"/>
                        <FooterStyle Font-Size="12px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="dueDate" DataFormatString="{0:d}" HeaderText="Due By" HtmlEncode="False" >
                        <ItemStyle Font-Size="Small" VerticalAlign="Top" />
                        <HeaderStyle Font-Size="Small" HorizontalAlign="Left" Width="65px" />
                    </asp:BoundField>
</Columns>
            <pagerSettings Mode="NumericFirstLast" />
        </asp:GridView>


Code Behind:

Protected Sub grdItems_Sorting(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdItems.Sorting
        Dim sortExpression As String = e.SortExpression
        ViewState("SortExpression") = sortExpression

        If grdItems.SortDirection = WebControls.SortDirection.Ascending Then
            grdItems.Sort(sortExpression, WebControls.SortDirection.Descending)
        Else
            grdItems.Sort(sortExpression, WebControls.SortDirection.Ascending)
        End If

    End Sub


Now, I know that, since I'm using Sort() inside the Sorting event handler, it's throwing me that exception. Is there any other way around this? Any help would be greatly appreciated. Thanks!
Posted
Updated 1-Nov-18 20:39pm

Simple: don't call Sort inside the Sorting event handler - because it'll raise a Sorting event, which will call the Sort method, which will raise the Sorting event, which ...
You get the idea.

Why would you want to sort in the middle of sorting anyway?
 
Share this answer
 
Comments
Member 13952925 1-Nov-18 16:18pm    
How would I sort a particular column then?
OriginalGriff 2-Nov-18 4:05am    
See here:
https://www.codeproject.com/Tips/663532/How-to-Perform-Sorting-in-Gridview-in-ASP-NET
You don't need to sort inside sorting event. You just bind the grid with new sortexpression.


Following is not needed:
grdItems.SortDirection = WebControls.SortDirection.Ascending Then
           grdItems.Sort(sortExpression, WebControls.SortDirection.Descending)
       Else
           grdItems.Sort(sortExpression, WebControls.SortDirection.Ascending)
       End If


Write something like this instead:
If grdItems.SortDirection = WebControls.SortDirection.Ascending Then
	grdItems.sortexpression =sortExpression + " " +  WebControls.SortDirection.Descending
Else
	grdItems.sortexpression =sortExpression + " " +  WebControls.SortDirection.Ascending
End If
grdItems.databind()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900