Results 1 to 4 of 4

Thread: [Resolved] GridView Sorting Problems

  1. #1

    Thread Starter
    Fanatic Member snufse's Avatar
    Join Date
    Jul 2004
    Location
    Jupiter, FL
    Posts
    912

    [Resolved] GridView Sorting Problems

    I have a GridView where I am using BoundFields. When I click sorting on a bound data field, it gives me error:

    An unhandled exception of type 'System.StackOverflowException' occurred in System.Web.dll. Make sure you do not have an infinite loop.

    Code:
    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
    
            Dim expression As String = e.SortExpression
            Dim direction As SortDirection = SortDirection.AscendingOrder
    
            GridView1.Sort(expression, direction)
            GridView1.DataSource = mySqlDataSet
            GridView1.DataBind()
    
    End Sub

    Code:
    <asp:GridView ID="GridView1" runat="server" Height="505px" Style="z-index: 10; left: -7px;
                position: absolute; top: 85px; border-right: gray thin solid; border-top: gray thin solid; border-left: gray thin solid; border-bottom: gray thin solid;" Width="902px" AllowPaging="True" AllowSorting="True" Font-Size="Small" PageSize="15" AutoGenerateColumns="False" CellSpacing="2" HorizontalAlign="Left" BorderColor="LightGray" BorderStyle="None" 
                onSorting="GridView1_Sorting" onrowcommand="GridView1_RowCommand">
                <PagerSettings PageButtonCount="15" />
                <RowStyle Height="6px" />
                <HeaderStyle BackColor="#FFE0C0" Height="6px" HorizontalAlign="Left" VerticalAlign="Middle" />
                <AlternatingRowStyle BackColor="#FFFFC0" />
                <Columns>
                    <asp:CommandField HeaderText="Select" SelectText="Ticket" ShowSelectButton="True" />
                    <asp:BoundField DataField="job_date" DataFormatString="{0:d}" HeaderText="Job Date" SortExpression="JobDate" >
                        <ItemStyle Font-Size="Small" HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="job_number" HeaderText="Job Number" SortExpression="JobNmber" />
                    <asp:BoundField DataField="cost_code" HeaderText="Cost Code" SortExpression="CostCode" />
                    <asp:BoundField DataField="qty_delivered" HeaderText="Qty Delivered - Asphalt" ReadOnly="True">
                        <ItemStyle HorizontalAlign="Right" Width="120px" />
                        <HeaderStyle Wrap="False" />
                    </asp:BoundField>
                    <asp:BoundField DataField="qty_received" HeaderText="Qty Received - Job">
                        <ItemStyle HorizontalAlign="Right" Width="120px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="qty_used" HeaderText="Qty Used - Job">
                        <ItemStyle HorizontalAlign="Right" Width="120px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="qty_wasted" HeaderText="Qty Wasted - Job">
                        <ItemStyle HorizontalAlign="Right" Width="120px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="plant_id" HeaderText="Plant Id" SortExpression="PlantId" />
                </Columns>
                <SelectedRowStyle BackColor="White" />
    </asp:GridView>
    Last edited by snufse; May 14th, 2008 at 11:23 AM.

  2. #2
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    Re: GridView Sorting Problems

    Code:
    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
    
            Dim expression As String = e.SortExpression
            Dim direction As SortDirection = SortDirection.AscendingOrder
    
            GridView1.Sort(expression, direction)
            GridView1.DataSource = mySqlDataSet
            GridView1.DataBind()
    
    End Sub
    That line is calling the method that it is in. Unfortunately, I don't have a databound website set up in asp.net right now that I can find the verbage you need. But that is the line causing the infinite loop.

  3. #3

    Thread Starter
    Fanatic Member snufse's Avatar
    Join Date
    Jul 2004
    Location
    Jupiter, FL
    Posts
    912

    Re: GridView Sorting Problems

    OK, I understand. So I have changed the code to:

    Code:
    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
    
            GridView1.DataSource = mySqlDataSet.Tables("JobListTable")
            GridView1.DataBind()
            
    End Sub
    When I debug I see that the "sortexpression" passed in is correct. From here I am not able to make the grid sort on the expression.

    My reading has been (http://msdn.microsoft.com/en-us/library/hwf94875.aspx)

    At run time, users can click the LinkButton control in a column heading to sort by that column. Clicking the link causes the page to perform a postback and raises the GridView control's Sorting event. The sort expression — by default, the name of the data column — is passed as part of the event arguments. The default behavior for the Sorting event is that the GridView control passes the sort expression to the data source control. The data source control executes its selection query or method, including the sort parameters passed by the grid.
    Last edited by snufse; May 13th, 2008 at 02:37 PM.

  4. #4

    Thread Starter
    Fanatic Member snufse's Avatar
    Join Date
    Jul 2004
    Location
    Jupiter, FL
    Posts
    912

    Re: [Resolved] GridView Sorting Problems

    Here is the code that will let me do sorting:

    Code:
    Private Property GridViewSortDirection() As String
            Get
                Return IIf(ViewState("SortDirection") = Nothing, "ASC", ViewState("SortDirection"))
            End Get
            Set(ByVal value As String)
                ViewState("SortDirection") = value
            End Set
        End Property
    
        Private Property GridViewSortExpression() As String
            Get
                Return IIf(ViewState("SortExpression") = Nothing, String.Empty, ViewState("SortExpression"))
            End Get
            Set(ByVal value As String)
                ViewState("SortExpression") = value
            End Set
        End Property
    
        Private Function GetSortDirection() As String
            Select Case GridViewSortDirection
                Case "ASC"
                    GridViewSortDirection = "DESC"
                Case "DESC"
                    GridViewSortDirection = "ASC"
            End Select
            Return GridViewSortDirection
    
        End Function
    
        Protected Sub gridViewPublishers_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
            GridView1.DataSource = SortDataTable(GridView1.DataSource, True)
            GridView1.PageIndex = e.NewPageIndex
            GridView1.DataBind()
    
        End Sub
    
        Protected Function SortDataTable(ByVal dataTable As Data.DataTable, ByVal isPageIndexChanging As Boolean) As Data.DataView
            If Not dataTable Is Nothing Then
                Dim dataView As New Data.DataView(dataTable)
                If GridViewSortExpression <> String.Empty Then
                    If isPageIndexChanging Then
                        dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection)
                    Else
                        dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GetSortDirection())
                    End If
                End If
                Return dataView
            Else
                Return New Data.DataView()
            End If
        End Function
    
        Protected Sub gridView1_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
            GridViewSortExpression = e.SortExpression
            CreateTextFile()
            AdsArrays()
            ReadAdsTables()
            FillTempTable()
            GridView1.DataSource = mySqlDataSet.Tables("JobListTable")
            Dim pageIndex As Integer = GridView1.PageIndex
            GridView1.DataSource = SortDataTable(GridView1.DataSource, False)
            GridView1.DataBind()
            GridView1.PageIndex = pageIndex
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width