3

現在、GridView でページを変更するときに並べ替えの設定を維持するコードを作成しようとしています。私の現在のコードは、ユーザーの並べ替えの選択に基づいてデータを並べ替えるために正常に機能しています。ただし、ページを変更すると、並べ替えが機能しません。エラーは発生しませんが、ソートされていない結果が表示されます。

これが私のaspxコードです:

<asp:GridView ID="tblAdministrators" runat="server" AutoGenerateColumns="false" EmptyDataText="No records found" PageSize="75" AllowPaging="True" AllowSorting="True">

  <Columns>
    <asp:BoundField HeaderText="Name" InsertVisible="False" DataField="FULLNAME" SortExpression="FULLNAME"></asp:BoundField>
  </Columns>
  <Columns>
   <asp:BoundField HeaderText="Active" InsertVisible="False" DataField="ACTIVE" SortExpression="ACTIVE"></asp:BoundField>
  </Columns>

</asp:GridView>

これが私のVBコードです:

Public Class ViewUsers
Inherits SolutionBasePage

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not Page.IsPostBack Then
        tblAdministrators.DataSource = GetData()
        tblAdministrators.DataBind()
    End If

End Sub

Private Const ASCENDING As String = " ASC"
Private Const DESCENDING As String = " DESC"

Private Function GetData() As Data.DataView

    Dim connection As OracleDBConnect = DAL.GetOracleDBConnection()
    Dim request As OracleDBRequest = Nothing
    Dim result As OracleDBResult = Nothing
    Dim trace As OracleDBChronoTrace = Nothing
    Dim status As DBStatus
    Dim sb As New StringBuilder
    Dim dv As DataView
    Dim ds As Data.DataSet = New Data.DataSet

    Try

        With sb
            .Append("SELECT FULLNAME, ACTIVE FROM USERS")
        End With

        request = New OracleDBRequest(sb.ToString, CommandType.Text)

        status = connection.Execute(request, result, trace)

        dv = New DataView(result.DataSet.Tables(0))

        If (ViewState("sortExp") IsNot Nothing) Then
            dv = New Data.DataView(result.DataSet.Tables(0))

            If (GridViewSortDirection = SortDirection.Ascending) Then
                GridViewSortDirection = SortDirection.Descending
                dv.Sort = CType(ViewState("sortExp") & DESCENDING, String)
            Else
                GridViewSortDirection = SortDirection.Ascending
                dv.Sort = CType(ViewState("sortExp") & ASCENDING, String)
            End If
        Else
            dv = result.DataSet.Tables(0).DefaultView
        End If

        Return dv

    Catch ex As Exception

    Finally
        'ds.Dispose()
        'dp.Dispose()
    End Try

End Function

Public Property GridViewSortDirection() As SortDirection
    Get
        If ViewState("sortDir") Is Nothing Then
            ViewState("sortDir") = SortDirection.Ascending
        End If

        Return CType(ViewState("sortDir"), SortDirection)
    End Get

    Set(ByVal value As SortDirection)
        ViewState("sortDir") = value
    End Set

End Property


Protected Sub tblAdministrators_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles tblAdministrators.PageIndexChanging

    tblAdministrators.PageIndex = e.NewPageIndex
    tblAdministrators.DataSource = GetData()
    tblAdministrators.DataBind()

End Sub


Protected Sub tblAdministrators_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles tblAdministrators.Sorting

    ViewState("sortExp") = e.SortExpression
    tblAdministrators.DataSource = GetData()
    tblAdministrators.DataBind()

End Sub

End Class
4

1 に答える 1

3

問題は、GetData() 関数を呼び出すたびに、PageIndexChaging イベントによって呼び出されている場合でも、コードがデータビューの並べ替えプロパティを変更していることです。

If (GridViewSortDirection = SortDirection.Ascending) Then
    GridViewSortDirection = SortDirection.Descending
    dv.Sort = CType(ViewState("sortExp") & DESCENDING, String)
Else
    GridViewSortDirection = SortDirection.Ascending
    dv.Sort = CType(ViewState("sortExp") & ASCENDING, String)
End If

GetData() 関数を変更して、SortDirection を受け入れることができます。次に、tblAdministrators_PageIndexChanging および tblAdministrators_Sorting イベントで、GridViewSortDirection を渡すだけです。このようなもの:

Private Function GetData(sort As SortDirection) As Data.DataView
' your code here...
End Function

Protected Sub tblAdministrators_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles tblAdministrators.PageIndexChanging
    tblAdministrators.PageIndex = e.NewPageIndex
    tblAdministrators.DataSource = GetData(GridViewSortDirection)
    tblAdministrators.DataBind()
End Sub

Protected Sub tblAdministrators_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles tblAdministrators.Sorting
    ViewState("sortExp") = e.SortExpression
    GridViewSortDirection = If(GridViewSortDirection = SortDirection.Descending, SortDirection.Ascending, SortDirection.Descending)
    tblAdministrators.DataSource = GetData(GridViewSortDirection)
    tblAdministrators.DataBind()
End Sub
于 2012-09-28T20:42:48.930 に答える