Results 1 to 2 of 2

Thread: [RESOLVED] Not getting indication of gridview's sort column

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Resolved [RESOLVED] Not getting indication of gridview's sort column

    I got this code off MSDN, and it's not working! (Am I the only one who's shocked?!)

    When the user sorts a gridview column, I'd like to give him an indication of what column he has sorted on, in case he's an idiot, I guess (actually, my primary user says who cares but since I'm 3/4 of the way there maybe I'll do it anyway, unless none of you cares enough to help! It's what the ASP version of our app is currently doing.)

    So here's the code:
    Code:
        ' Source: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sortexpression(VS.80).aspx
        Protected Sub gvDocList_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDocList.RowCreated
    
            ' Use the RowType property to determine whether the 
            ' row being created is the header row. 
            If e.Row.RowType = DataControlRowType.Header Then
    
                ' Call the GetSortColumnIndex helper method to determine
                ' the index of the column being sorted.
                Dim sortColumnIndex As Integer = GetSortColumnIndex()
    
                If sortColumnIndex <> -1 Then
    
                    ' Call the AddSortImage helper method to add
                    ' a sort direction image to the appropriate
                    ' column header. 
                    AddSortImage(sortColumnIndex, e.Row)
    
                End If
    
            End If
    
        End Sub
    
        ' This is a helper method used to determine the index of the
        ' column being sorted. If no column is being sorted, -1 is returned.
        Function GetSortColumnIndex() As Integer
    
            ' Iterate through the Columns collection to determine the index
            ' of the column being sorted.
            Dim field As DataControlField
            For Each field In gvDocList.Columns
    
                ' md Added first condtion because it was saying I was sorting on my "Selected" checkbox column which 
                '  isn't even sortable.
                If field.SortExpression <> "" And field.SortExpression = gvDocList.SortExpression Then
    
                    Return gvDocList.Columns.IndexOf(field)
    
                End If
    
            Next
    
            Return -1
        End Function
    
        ' This is a helper method used to add a sort direction
        ' image to the header of the column being sorted.
        Sub AddSortImage(ByVal columnIndex As Integer, ByVal row As GridViewRow)
    
            ' Create the sorting image based on the sort direction.
            Dim sortImage As New Image()
            If gvDocList.SortDirection = SortDirection.Ascending Then
    
                sortImage.ImageUrl = "~/Images/UpArrow.gif"
                sortImage.AlternateText = "Ascending Order"
    
            Else
    
                sortImage.ImageUrl = "~/Images/DownArrow.gif"
                sortImage.AlternateText = "Descending Order"
    
            End If
            ' Add the image to the appropriate header cell.
            row.Cells(columnIndex).Controls.Add(sortImage)
    
        End Sub
    I added this one change:
    ' md Added first condtion because it was saying I was sorting on my "Selected" checkbox column which
    ' isn't even sortable.
    If field.SortExpression <> "" And field.SortExpression = gvDocList.SortExpression Then

    because it was going through the columns but always getting a match on the first one when my first column isn't even sortable.

    Is this code no good?

    In my ASP I have AllowSorting=True and on the columns I want to sort on I have SortExpression set (sorting is working fine, just not the extra touch the MS code was supposed to provide me).

    Thanks.

  2. #2

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Not getting indication of gridview's sort column

    It is now working.

    I commented out that code I had in RowCreated.

    Here's the code I have:
    Code:
        ' The GridViewSortDirection is a simple property which returns the new sort direction for the GridView control. 
        '  Since, the header of the column triggers a postback that is why I am saving the last sort direction into the 
        '  ViewState object. Once, I know the last direction I can give the user the new sort direction. This means 
        '  that if the column was sorted in ascending order then the new direction has to be descending. 
        Public Property GridViewSortDirection() As SortDirection
    
            Get
                If ViewState("sortDirection") Is Nothing Then
                    ViewState("sortDirection") = SortDirection.Ascending
                End If
    
                Return DirectCast(ViewState("sortDirection"), SortDirection)
            End Get
    
            Set(ByVal value As SortDirection)
                ViewState("sortDirection") = value
            End Set
        End Property
    
        ' Marlene added to remember what column we're sorted on.  If it's nothing, default it to dt.      
    Public Property GridViewSortColumn() As String
    
            Get
                If ViewState("sortColumn") Is Nothing Then
                    ViewState("sortColumn") = "dt"
                End If
    
                Return DirectCast(ViewState("sortColumn"), String)
            End Get
    
            Set(ByVal value As String)
                ViewState("sortColumn") = value
            End Set
        End Property
        ' End of Marlene added.
    
        Protected Sub gvDocList_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvDocList.Sorting
            Dim sortExpression As String = e.SortExpression
    
            If GridViewSortDirection = SortDirection.Ascending Then
    
                GridViewSortDirection = SortDirection.Descending
    
                GridViewSortColumn = e.SortExpression
    
                SortGridView(sortExpression, "DESC")
            Else
                GridViewSortDirection = SortDirection.Ascending
    
                GridViewSortColumn = e.SortExpression
    
                SortGridView(sortExpression, "ASC")
            End If
    
        End Sub
    
        Private Sub SortGridView(ByVal sortExpression As String, ByVal direction As String)
    
            Try
    
                Dim dt As DataTable = ds.Tables(0)
    
                Dim dv As New DataView(dt)
    
                ' A string that contains the column name followed by "ASC" (ascending) or "DESC" (descending). 
                '  Columns are sorted ascending by default. Multiple columns can be separated by commas. 
                ' This is an example of how to set the property:
                '  view.Sort = "State, ZipCode DESC
                dv.Sort = sortExpression + " " + direction
    
                ' Give an indication of what column is sorted on.  Reset all headers back to their default.
                Me.gvDocList.Columns(1).HeaderText = "Col1"
                Me.gvDocList.Columns(2).HeaderText = "Col2"
                Me.gvDocList.Columns(3).HeaderText = "Col3"
                Me.gvDocList.Columns(4).HeaderText = "Col4"
                Me.gvDocList.Columns(5).HeaderText = "Col5"
                For i As Integer = 1 To 5
                    Me.gvDocList.Columns(i).HeaderStyle.Font.Bold = False
                Next i
    
                ' Then flag the sorted columns.
                Select Case sortExpression
                    Case "a"
                        Me.gvDocList.Columns(1).HeaderText = "Col1 (" + direction + ")"
                        Me.gvDocList.Columns(1).HeaderStyle.Font.Bold = True
                    Case "b"
                        Me.gvDocList.Columns(2).HeaderText = "Col2 (" + direction + ")"
                        Me.gvDocList.Columns(2).HeaderStyle.Font.Bold = True
                    Case "c"
                        Me.gvDocList.Columns(3).HeaderText = "Col3 (" + direction + ")"
                        Me.gvDocList.Columns(3).HeaderStyle.Font.Bold = True
                    Case "d"
                        Me.gvDocList.Columns(4).HeaderText = "Col4 (" + direction + ")"
                        Me.gvDocList.Columns(4).HeaderStyle.Font.Bold = True
                    Case "e"
                        Me.gvDocList.Columns(5).HeaderText = "Col5 (" + direction + ")"
                        Me.gvDocList.Columns(5).HeaderStyle.Font.Bold = True
                End Select
    
                gvDocList.DataSource = dv
    
                gvDocList.DataBind()
    
            Catch ex As Exception
                Response.Write("Exception in SortGridView(): " + ex.Message)
            End Try
    
        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