简体   繁体   中英

gridview sorting based on columns

Hi i have a grid view in this i am displaying few columns called name, phone, information and date. My requirement is if I click on the columns header then it should be sorted, my aspx code is here...

<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>

and my code is:

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

for this if i click on the column header it should sort first as ascending order if i click next time then it should sort in desc order it sholud apply for all the columns listed in grid view...

please help me in coding for this...

You need to handle OnSorting event, here is how I'm doing it:

ASPX:

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

CodeBehind:

//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
}

now in FillGrid you need to sort the table (details) you are getting based on these values:

    '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()

This is the code in vb.net. You need to handle the Sorting event for the GridView and you also need to maintain view state for the previous sort.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM