I think I'm almost there... I hope!
What I did:
1) On the aspx.vb page:
Protected Sub gvSupplier_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvSupplier.Sorting
Dim PagSort As PagingSorting = New PagingSorting
PagSort.GridViewSortExpression = e.SortExpression
Dim pageIndex As Integer = Me.gvSupplier.PageIndex
Me.gvSupplier.DataSource = PagSort.SortDataTable(Me.gvSupplier.DataSource, False)
Me.gvSupplier.DataBind()
Me.gvSupplier.PageIndex = pageIndex
End Sub
Protected Sub gvSupplier_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvSupplier.PageIndexChanging
Dim PagSort As PagingSorting = New PagingSorting
Me.gvSupplier.DataSource = PagSort.SortDataTable(Me.gvSupplier.DataSource, True)
Me.gvSupplier.PageIndex = e.NewPageIndex
Me.gvSupplier.DataBind()
End Sub
2) I created an class page called PagingSorting.vb and add:
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Public Class PagingSorting
Inherits System.Web.UI.Page
Public Property GridViewSortDirection() As String
Get
Return IIf(ViewState("SortDirection") = Nothing, "ASC", ViewState("SortDirection"))
End Get
Set(ByVal value As String)
ViewState("SortDirection") = value
End Set
End Property
Public Property GridViewSortBLOCKED EXPRESSION As String
Get
Return IIf(ViewState("SortExpression") = Nothing, String.Empty, ViewState("SortExpression"))
End Get
Set(ByVal value As String)
ViewState("SortExpression") = value
End Set
End Property
Public Function GetSortDirection() As String
Select Case GridViewSortDirection
Case "ASC"
GridViewSortDirection = "DESC"
Case "DESC"
GridViewSortDirection = "ASC"
End Select
Return GridViewSortDirection
End Function
Public Function SortDataTable(ByVal dataTable As DataTable, ByVal isPageIndexChanging As Boolean) As DataView
If Not dataTable Is Nothing Then
Dim dataView As New DataView(dataTable)
If GridViewSortExpression <> String.Empty Then
If isPageIndexChanging Then
dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection)
Else
dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GetSortDirection())
End If
End If
Return dataView
Else
Return New DataView()
End If
End Function
End Class
The paging works fine, but the sorting doesn't. It only works on the first time. I did some debugging and realized that the PageIndex is always = 0.
How can I retrieve the right PageIndex?
Thank's a lot once again!
Paula