gridview 翻页后保持排序

 1 Protected Sub gvdatalist_Sorting(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvdatalist.Sorting
 2
 3        GridViewSortExpression = e.SortExpression
 4
 5        Dim pageIndex As Integer = gvdatalist.PageIndex
 6        gvdatalist.DataSource = SortDataTable(dt, False)
 7        gvdatalist.DataBind()
 8        gvdatalist.PageIndex = pageIndex
 9
10    End Sub

11
12    Private Property GridViewSortDirection() As String
13        Get
14            Return ViewState("SortDirection")
15        End Get
16        Set(ByVal Value As String)
17            ViewState("SortDirection"= value
18        End Set
19    End Property

20
21
22
23    Private Property GridViewSortExpression() As String
24        Get
25            Return ViewState("SortExpression")
26        End Get
27        Set(ByVal Value As String)
28            ViewState("SortExpression"= value
29        End Set
30    End Property

31
32
33
34    Private Function GetSortDirection() As String
35
36        Select Case GridViewSortDirection
37
38            Case "ASC"
39                GridViewSortDirection = "DESC"
40
41                'Exit Function
42
43            Case "DESC"
44
45                GridViewSortDirection = "ASC"
46
47                ' Exit Function
48            Case Else
49                GridViewSortDirection = "ASC"
50
51        End Select
52        Return GridViewSortDirection
53
54    End Function

55
56
57
58
59    Protected Function SortDataTable(ByVal dataTable As DataTable, ByVal isPageIndexChanging As BooleanAs DataView
60
61        If Not dataTable Is Nothing Then
62
63            Dim dataView As DataView = New DataView(dataTable)
64
65            If GridViewSortExpression <> String.Empty Then
66
67                If isPageIndexChanging Then
68
69                    dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection)
70
71                Else
72
73                    dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GetSortDirection())
74
75                End If
76
77            End If
78
79            Return dataView
80
81        Else
82
83            Return New DataView()
84
85        End If
86
87    End Function

88
89    Protected Sub gvdatalist_PageIndexChanging(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvdatalist.PageIndexChanging
90        Me.gvdatalist.PageIndex = e.NewPageIndex
91        Me.gvdatalist.DataSource = SortDataTable(dt, True)
92        Me.gvdatalist.DataBind()
93        Me.drto.SelectedValue = e.NewPageIndex + 1
94    End Sub
posted @ 2008-05-28 17:40  hambywu  阅读(646)  评论(0编辑  收藏  举报