将CSSClass应用于GridView排序 [英] Apply CSSClass For GridView Sorting

查看:78
本文介绍了将CSSClass应用于GridView排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET 4.0和VB.NET开发了一个应用程序,我想在对GridView进行排序时为GridView Header单元添加图像(升序和降序排序).
当我在Google上搜索此内容时,我发现ASP.NET 4支持新的Sort Style,如下所示:


SortedAscendingCellStyle
SortedAscendingHeaderStyle
SortedDescendingCellStyle
SortedDescendingHeaderStyle

当我将它与SQLDataSource一起使用时,它可以正常工作,
但是当我使用它并在Page_Load事件(通过Coding(DataSet))中绑定GridView时,请为GridView自定义排序代码,这些样式将不起作用!!!

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 ?

推荐答案

我找到了这些情况的替代解决方案,我想与您分享
首先,我为CSS文件添加了以下代码行:
I found an alternative solution for these situation,I want to share it with you
First I added theses lines for my CSS file :
.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 :

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



然后更新了我的代码,以对gridview进行自定义排序,如下所示:

1. GridView排序事件的代码



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

1.Code for GridView Sorting Event

'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.GetSortDirectionString函数的代码



2.Code For GetSortDirectionString Function

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




就是这样!
感谢所有尝试帮助我的人:D




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


这篇关于将CSSClass应用于GridView排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆