Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


After uploding from Excel to Gridview my sorting is not working. Can anybody help me.

Thanks
Posted

1 solution

VB
Protected Sub Gridv_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles Gridv.Sorting
        Dim sortExpression As String = e.SortExpression
        ViewState("SortExpression") = sortExpression
        If SortDirection = SortDirection.Ascending Then
            SortDirection = SortDirection.Descending
            SortGridView(sortExpression, DESCENDING)
        Else
            SortDirection = SortDirection.Ascending
            SortGridView(sortExpression, ASCENDING)
        End If
    End Sub
    Private Property SortDirection() 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
  Private Sub SortGridView(ByVal sortExpression As String, ByVal direction As String)
        Dim con As OleDbConnection = Nothing
        Try
            con = New OleDbConnection(";Extended Properties=""Excel 8.0"";")
        Catch ex As Exception
            lt1.Text = ex.Message
            Exit Sub
        End Try
        Dim sSQL As String = "SELECT * FROM [Excel$];"
        Dim cmd As New OleDbDataAdapter(sSQL, con)
        Dim ds As New DataSet
        Try
            cmd.Fill(ds, sSQL)
            Dim dv As DataView = New DataView(ds.Tables("sSQL"))
            'UpdateColumnHeaders(Gridv)
            Gridv.DataSource = ds
            Gridv.DataBind()
            'If Not (ViewState("ssortexp").ToString() = "") Then
            '    dv.Sort = (ViewState("ssortexp").ToString() + ViewState("sorder").ToString())
            'End If
        Catch ex As Exception
            lt1.Text = ex.Message
            Exit Sub
        Finally
            con.Close()
        End Try
    End Sub
    Protected Sub Gridv_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.Header Then
            Dim sortColumnIndex As Integer = GetSortColumnIndex()
            If sortColumnIndex <> -1 Then
                AddSortImage(sortColumnIndex, e.Row)
            End If
        End If
    End Sub
    Private Function GetSortColumnIndex() As Integer
        For Each field As DataControlField In Gridv.Columns
            If field.SortExpression = DirectCast(ViewState("SortExpression"), String) Then
                Return Gridv.Columns.IndexOf(field)
            End If
        Next
        Return -1
    End Function
    Private Sub AddSortImage(ByVal columnIndex As Integer, ByVal headerRow As GridViewRow)
        ' Create the sorting image based on the sort direction.
        Dim sortImage As New Image()
        If SortDirection = SortDirection.Ascending Then
            sortImage.ImageUrl = "images/arrow.gif"
            sortImage.AlternateText = "Ascending Order"
        Else
            sortImage.ImageUrl = "images/arrowdown.gif"
            sortImage.AlternateText = "Descending Order"
        End If
        ' Add the image to the appropriate header cell.
        headerRow.Cells(columnIndex).Controls.Add(sortImage)
    End Sub
 
Share this answer
 
v2

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