Click here to Skip to main content
14,975,219 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I develop an application using ASP.NET 4.0 with VB.NET and I want to add images at GridView Header cells for (Sorting Ascending and Sorting Descending) when sorting the GridView
when I searched about this at Google ,I found that ASP.NET 4 support new Sort Style as following :


SortedAscendingCellStyle
SortedAscendingHeaderStyle
SortedDescendingCellStyle
SortedDescendingHeaderStyle

when I used it with SQLDataSource It works correctly ,
but when I used it and Binding the GridView in Page_Load Event (By Coding(DataSet)) ,so customize the Sorting Code for GridView these Styles Does NOT work !!!

is there any solution here ?
Posted

1 solution

I found an alternative solution for these situation,I want to share it with you
First I added theses lines for my CSS file :
CSS
.SortedAscendingHeaderStyle, .SortedDescendingHeaderStyle
{
  background-color: #FFFFCC;
  background-repeat: no-repeat;
}

.SortedAscendingHeaderStyle
{
  background-image: url(Controls/Images/arrow_up.png);
}

.SortedDescendingHeaderStyle
{
  background-image: url(Controls/Images/arrow_down.png);
}


then I added a new function to return the index of the sorted column as following :

VB
Private Function GetIndex(ByVal SortExp As String) As Integer

        Dim i As Integer = 0
        For Each c As DataControlField In GridViewRequests.Columns
            If c.SortExpression = SortExp Then
                Return i
            Else
                i += 1
            End If
        Next

        Return i
End Function


then updated my code for custom sorting the gridview to be as following :

1.Code for GridView Sorting Event
VB
'Note:FillDataSet() is a Function will Fill DataSet with data
'     To use it as DataSource For My GridView
Protected Sub GridViewRequests_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridViewRequests.Sorting
       Dim t As DataTable = CType(FillDataSet().Tables(0), DataTable)
       Dim dv As DataView = t.AsDataView
       dv.Sort = e.SortExpression + " " + GetSortDirectionString(e.SortExpression)
       GridViewRequests.DataSource = dv
       GridViewRequests.DataBind()

       'Apply CssClass for the sorting column
       If ViewState("SortDirection") = "ASC" Then
           GridViewRequests.HeaderRow.Cells(GetIndex(e.SortExpression)).CssClass = "SortedAscendingHeaderStyle"
       Else
           GridViewRequests.HeaderRow.Cells(GetIndex(e.SortExpression)).CssClass = "SortedDescendingHeaderStyle"
       End If

   End Sub


2.Code For GetSortDirectionString Function
VB
Private Function GetSortDirectionString(ByVal column As String) As String

        ' By default, set the sort direction to ascending.
        Dim sortDirection = "ASC"

        ' Retrieve the last column that was sorted.
        Dim sortExpression = TryCast(ViewState("SortExpression"), String)

        If sortExpression IsNot Nothing Then
            ' Check if the same column is being sorted.
            ' Otherwise, the default value can be returned.
            If sortExpression = column Then
                Dim lastDirection = TryCast(ViewState("SortDirection"), String)
                If lastDirection IsNot Nothing AndAlso lastDirection = "ASC" Then
                    sortDirection = "DESC"
                End If
            End If
        End If

        ' Save new values in ViewState.
        ViewState("SortDirection") = sortDirection
        ViewState("SortExpression") = column

        Return sortDirection
    End Function



That It!!
thank for all who try to help me :D
   

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