[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

GridView Sorting

Posted on 2006-04-27
5
Medium Priority
?
752 Views
Last Modified: 2010-05-18
Hi,
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" />
                                                    <Columns>
                                                        <asp:TemplateField SortExpression="UserID"  HeaderText="Select">
                                                         <ItemTemplate>
                                                           <a href="javascript: OpenNewWindow('users.aspx?userid=<%# Eval("UserID") %>','500','600','SD')"><%# Eval("UserID") %></a>
                                                         </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <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>
                                                        <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" />
                                                        </asp:BoundField>

                                                    </Columns>
                                                    <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" />
                                                </asp:GridView>



    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"
            Else
                Me.sortDir = "desc"
            End If
        End If

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


    ' Holds the column name to be sorted on...
    Public Property sortCriteria() As String
        Get
            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
        Get
            Return CStr(ViewState("sortDir"))
        End Get
        Set(ByVal Value As String)
            ViewState("sortDir") = Value
        End Set
    End Property
0
Comment
Question by:Carl3003
  • 2
3 Comments
 
LVL 3

Expert Comment

by:pitster
ID: 16553057
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")
        Else
            If viewstate("sortdirection") = "ASC" Then
                viewstate("sortdirection") = "DESC"
            Else
                viewstate("sortdirection") = "ASC"
            End If
        End If
        Session("lastColSorted") = e.SortExpression.ToString & " " & viewstate("sortdirection").ToString()
        tblView.Sort = e.SortExpression.ToString & " " & viewstate("sortdirection").ToString()
        DataGrid1.DataBind()
    End Sub



Good luck!
0
 

Author Comment

by:Carl3003
ID: 16553236
It was not working for me. Is sorting for datagrid and gridview the same?
0
 
LVL 3

Accepted Solution

by:
pitster earned 1000 total points
ID: 16555440
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

        YourDataAdapter.Fill(DataSet1)
        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")
        DataGrid1.DataBind()
    End Sub



Good Luck
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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