0

嗨,我有一个网格视图,我显示了几个名为姓名、电话、信息和日期的列。我的要求是,如果我点击列标题,那么它应该被排序,我的 aspx 代码在这里......

<asp:GridView ID="sorttest" runat="server" AllowSorting="true">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="Sno." Visible="False" />
        <asp:BoundField DataField="Name" HeaderStyle-CssClass="tdHead3" HeaderText="Name" SortExpression="Name" />
        <asp:BoundField DataField="Phone" HeaderStyle-CssClass="tdHead3" HeaderText="Phone Number" SortExpression="Phone"/>
        <asp:BoundField DataField="Information" HeaderStyle-CssClass="tdHead3" HeaderText="Information Of The Offender" SortExpression="Information"/>
        <asp:BoundField DataField="Date" HeaderStyle-CssClass="tdHead3" HeaderText="Date of Graffiti" SortExpression="Date"/>
    </Columns>
</asp:GridView>

我的代码是:

Public Sub FillGrid()
    Try
        myconnection = New SqlConnection(conntection string)
        cmd = New SqlCommand()
        cmd.CommandText = "Retrievedetails"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = myconnection
        ds = New DataSet()
        da = New SqlDataAdapter(cmd)
        da.Fill(ds, "details")
        myconnection.Open()
        sorttest.DataSource = ds
        sorttest.DataBind()
    Catch ex As Exception
    Finally
        myconnection.Close()
    End Try
End Sub

为此,如果我单击列标题,它应该首先按升序排序,如果我下次单击,它应该按降序排序,它应该适用于网格视图中列出的所有列...

请帮助我为此编码...

4

2 回答 2

1

您需要处理OnSorting事件,这是我的做法:

ASPX:

 <asp:GridView ID="sorttest" runat="server" AllowSorting="true"
    OnSorting="SortGrid">

代码隐藏:

//sorry I'm putting my CS code, as translating it to VB will be 
//very tedious for me
protected void SortGrid(object sender, GridViewSortEventArgs e)
{
    ViewState["SortDirection"] = 
          (e.SortDirection == SortDirection.Ascending) ? "asc": "desc";

    ViewState["SortExp"] = e.SortExpression;

    FillGrid();//CALL FILL GRID
}

现在FillGrid您需要根据这些值对您获得的表(详细信息)进行排序:

    'Here sort the table (details) based on values stored in View State
    'Sorry My VB skills are really rusty now :)

    var dv as DataView = ds.Tables("details").DefaultView;

    if not (ViewState("SortExp") is nothing) then 'check for null
        if ViewState("SortDirection").ToString() == "asc" then
            dv.Sort = ViewState("SortExp") & " " & "asc"
        else
            dv.Sort = ViewState("SortExp") & " " & "desc"
        end if
    end if

    sorttest.DataSource = dv 'bind to dataview
    sorttest.DataBind()
于 2009-10-09T06:57:31.560 回答
1

这是 vb.net 中的代码。您需要处理 GridView 的 Sorting 事件,还需要维护前一个排序的视图状态。

Private Property PreviousSortField() As String
    Get
        If ViewState("SortField") Is Nothing Then
            Return ""
        Else
            Return ViewState("SortField")
        End If
    End Get
    Set(ByVal value As String)
        ViewState("SortField") = value
    End Set
End Property

Private Property PreviousSortDirection() As String
    Get
        If ViewState("SortDirection") Is Nothing Then
            Return ""
        Else
            Return ViewState("SortDirection")
        End If
    End Get
    Set(ByVal value As String)
        ViewState("SortDirection") = value
    End Set
End Property

Protected Sub sorttest_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles sorttest.Sorting
    Dim SortDirection As String
    If e.SortDirection = WebControls.SortDirection.Ascending Then
        SortDirection = "DESC"
    ElseIf e.SortDirection = WebControls.SortDirection.Descending Then
        SortDirection = "ASC"
    Else
        SortDirection = "ASC"
    End If
    Dim ds As DataSet = sender.Datasource
    Dim Data As DataTable = ds.Tables(0)
    If Not Data Is Nothing Then
        Dim dv As DataView = New DataView(Data)
        If e.SortExpression = PreviousSortField And SortDirection = PreviousSortDirection Then
            If SortDirection.ToUpper = "ASC" Then
                SortDirection = "DESC"
            ElseIf SortDirection.ToUpper = "DESC" Then
                SortDirection = "ASC"
            Else
                SortDirection = "ASC"
            End If
        End If
        dv.Sort = e.SortExpression & " " & SortDirection
        sender.DataSource = dv
        sender.DataBind()
    End If

    'Need to store sort info in view state
    PreviousSortField = e.SortExpression
    PreviousSortDirection = SortDirection
End Sub
于 2009-10-09T17:13:39.903 回答