基于列的gridview排序

时间:2009-10-09 06:29:32

标签: asp.net sorting gridview sortdirection

嗨我有一个网格视图,我显示的名称,电话,信息和日期列很少。 我的要求是如果我点击列标题然后它应该排序,我的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

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

请帮我编码......

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分: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