GridView Sorting

Posted on 2006-04-27
Last Modified: 2010-05-18
I guess i am missing something bc sorting does not work.

                                                <asp:GridView ID="gridUserInfo" AllowPaging="True" AllowSorting="True"  runat="server"
                                                AutoGenerateColumns="False" CellPadding="4" DataKeyNames="UserID"
                                                ForeColor="#333333" GridLines="None"
                                                OnPageIndexChanging="gridUserInfo_PageIndexChanging" OnSorting ="gridUserInfo_OnSorting" >
                                                    <PagerSettings PageButtonCount="15" Position="TopAndBottom" />
                                                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                                        <asp:TemplateField SortExpression="UserID"  HeaderText="Select">
                                                           <a href="javascript: OpenNewWindow('users.aspx?userid=<%# Eval("UserID") %>','500','600','SD')"><%# Eval("UserID") %></a>
                                                        <asp:BoundField DataField="FirstName" HeaderText="First Name" NullDisplayText="n/a">
                                                            <ItemStyle Font-Names="verdana" Font-Size="XX-Small" Width="66px" />
                                                            <HeaderStyle Font-Names="Verdana" Font-Size="XX-Small" Height="15px" HorizontalAlign="Left" />
                                                        <asp:BoundField DataField="LastName" HeaderText="Last Name" />
                                                        <asp:BoundField DataField="EmailId" HeaderText="Email">
                                                            <ItemStyle Font-Names="verdana" Font-Size="XX-Small" Width="100px" />
                                                            <HeaderStyle Font-Names="verdana" Font-Size="XX-Small" Height="15px" HorizontalAlign="Left" />

                                                    <RowStyle BackColor="#EFF3FB" />
                                                    <EditRowStyle BackColor="#2461BF" CssClass="TierBNBTitleFont" />
                                                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                                                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                                                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" Font-Names="verdana" Font-Size="XX-Small" />
                                                    <AlternatingRowStyle BackColor="White" />

    Protected Sub gridUserInfo_OnSorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
        ' Check to see if the column we clicked on is the current column defined
        ' in the sortCriteria property.
        If Me.sortCriteria = e.SortExpression Then
            If Me.sortDir = "desc" Then
                Me.sortDir = "asc"
                Me.sortDir = "desc"
            End If
        End If

        ' Assign the column clicked to the sortCriteria property
        Me.sortCriteria = e.SortExpression
        gridUserInfo.DataSource = LoadData(Session("uId"))
    End Sub

    ' Holds the column name to be sorted on...
    Public Property sortCriteria() As String
            Return CStr(viewstate("sortCriteria"))
        End Get
        Set(ByVal Value As String)
            ViewState("sortCriteria") = Value
        End Set
    End Property

    ' Holds the direction to be sorted...
    Public Property sortDir() As String
            Return CStr(ViewState("sortDir"))
        End Get
        Set(ByVal Value As String)
            ViewState("sortDir") = Value
        End Set
    End Property
Question by:Carl3003
    LVL 3

    Expert Comment

    Here is a piece of code I used in the past:

    Private Sub DataGrid1_SortCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
            viewstate.Add("sortfield", e.SortExpression)
            If viewstate("sortdirection") Is Nothing Then
                viewstate.Add("sortdirection", "ASC")
                If viewstate("sortdirection") = "ASC" Then
                    viewstate("sortdirection") = "DESC"
                    viewstate("sortdirection") = "ASC"
                End If
            End If
            Session("lastColSorted") = e.SortExpression.ToString & " " & viewstate("sortdirection").ToString()
            tblView.Sort = e.SortExpression.ToString & " " & viewstate("sortdirection").ToString()
        End Sub

    Good luck!

    Author Comment

    It was not working for me. Is sorting for datagrid and gridview the same?
    LVL 3

    Accepted Solution

    Sorry I forgot to put in the page_load:
    Public tblView as DataView
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here

            tblView = Dataset1.YourTableName
            If Not Page.IsPostBack Then
                Session("lastColSorted") = "YourColumnName DESC" 'This is just for a default sort
            End If
            tblView.Sort = Session("lastColSorted")
        End Sub

    Good Luck

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    method argument as final 1 54
    scores100 challenge 3 68
    triangle challenge 4 41
    Installed softare without GUID 6 30
    This is an explanation of a simple data model to help parse a JSON feed
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now