Gridview在列标题中对向上/向下箭头进行排序

时间:2012-10-03 14:49:37

标签: asp.net vb.net sorting gridview paging

我目前正在研究一些代码,它负责在网格视图中对数据进行排序和分页。我现在正在尝试实现排序箭头(列标题旁边的向上/向下箭头),但我没有成功。实现排序箭头的代码位于* GridView1_RowCreated *子例程中。现在,当我运行代码时,我根本看不到排序箭头。有问题的行是以下一行:“如果tblAdministrators.SortExpression = lnk.CommandArgument那么”我无法弄清楚该行有什么问题。它总是 False 因此箭头不显示。

Private Function GetData(sort As SortDirection) 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

    With sb
        .Append("SELECT * 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").ToString() & DESCENDING, String)
        Else
            GridViewSortDirection = SortDirection.Ascending
            dv.Sort = CType(ViewState("sortExp").ToString() & ASCENDING, String)
        End If
    Else
        dv = result.DataSet.Tables(0).DefaultView
    End If

    Return dv

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 GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles tblAdministrators.PageIndexChanging

    tblAdministrators.PageIndex = e.NewPageIndex
    GridViewSortDirection = If(GridViewSortDirection = SortDirection.Descending, SortDirection.Ascending, SortDirection.Descending)
    tblAdministrators.DataSource = GetData(GridViewSortDirection)
    tblAdministrators.DataBind()

End Sub


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

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

End Sub

Protected Sub GridView1_RowCreated(sender As Object, e As GridViewRowEventArgs) Handles tblAdministrators.RowCreated
    If e.Row.RowType = DataControlRowType.Header Then
        For Each tc As TableCell In e.Row.Cells
            If tc.HasControls() Then

                Dim lnk As LinkButton = DirectCast(tc.Controls(0), LinkButton)
                If lnk IsNot Nothing Then

                    Dim img As New System.Web.UI.WebControls.Image()

                    img.ImageUrl = "/images/" & (If(GridViewSortDirection = SortDirection.Ascending, "asc", "desc")) & ".gif"

                    If tblAdministrators.SortExpression = lnk.CommandArgument Then

                        tc.Controls.Add(New LiteralControl(" "))
                        tc.Controls.Add(img)

                    End If
                End If
            End If
        Next
    End If
End Sub

ASPX代码:

 <asp:GridView ID="tblAdministrators" runat="server" AutoGenerateColumns="false" EmptyDataText="No records found" PageSize="25" AllowPaging="True" AllowSorting="True" OnRowCreated="GridView1_RowCreated">
        <Columns>
            <asp:BoundField HeaderText="Name" DataField="Name" SortExpression="NAME"></asp:BoundField>
        </Columns>
        <Columns>
            <asp:BoundField HeaderText="City" DataField="City" SortExpression="CITY"></asp:BoundField>
        </Columns>
    </asp:GridView>

2 个答案:

答案 0 :(得分:4)

看看我的样本:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerId" 
    DataSourceID="SqlDataSource1">
    <SortedAscendingHeaderStyle CssClass="sortasc" />
    <SortedDescendingHeaderStyle CssClass="sortdesc" />
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="CustomerId" HeaderText="CustomerId" 
            InsertVisible="False" ReadOnly="True" SortExpression="CustomerId" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        <asp:BoundField DataField="IdNumber" HeaderText="IdNumber" 
            SortExpression="IdNumber" />
    </Columns>
</asp:GridView>

注意SortedAscendingHeaderStyle和SortedDescendingHeaderStyle。只需使用背景图像(向上箭头和向下箭头)创建适当的css类,您就完成了。

答案 1 :(得分:0)

为了在VB.net Windows窗体上的数据网格中添加排序箭头,您甚至不必编写任何代码

  1. 在设计模式下的表单上 - 选择您正在使用的网格,右键单击转到属性,设置选择模式=单元格选择
  2. 在设计模式下的表单上 - 单击右上方网格上的箭头,编辑列属性,选择编辑或添加列,设置SortMode = Automatic
  3. 如果您有单元格内容的代码(假设您希望用户在单击数据网格中的任何行时打开另一个表单),请使用CellDoubleClick属性来处理该事件。如果您使用了单击,那么它将对标题进行排序,并打开给定单元格的表单
  4. Private Sub _CellDoubleClick(sender As Object,e As DataGridViewCellEventArgs)处理DGAcctGrpList.CellDoubleClick