Results 1 to 6 of 6

Thread: [RESOLVED] Grid view Sorting

  1. #1

    Thread Starter
    Lively Member achor's Avatar
    Join Date
    May 2006
    Location
    Porto
    Posts
    123

    Resolved [RESOLVED] Grid view Sorting

    Hello all,

    I've searched here and in google for my problem. But what I didn´t found what i was looking for (lol looks like the U2 song)...

    Anyway, I have a gridview with paging implemented.

    And now what i want is to add the sorting feature to it.

    So far I have:

    Code:
    <asp:GridView ID="GridView1" allowpaging="True" allowsorting="True" runat="server"
                    CssClass="DataWebControlStyle" CellPadding="4" Font-Names="Verdana" Font-Size="X-Small" 
                    AutoGenerateColumns="False" ForeColor="#333333" GridLines="None" 
    Width="496px" CellSpacing="2" 
                    PageSize="15" datakeynames="NID" 
    emptydatatext="No data available."   
                    onpageindexchanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting">
                    
                    <HeaderStyle CssClass="HeaderStyle" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <AlternatingRowStyle CssClass="AlternatingRowStyle" BackColor="White" ForeColor="#284775" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <EditRowStyle BackColor="#999999" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    
                    <Columns>
                        <asp:BoundField DataField="Product" HeaderText="Product"><ControlStyle Width="40px" />
                            <ItemStyle Width="200px" />
                        </asp:BoundField>
                        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="name" >
                            <ItemStyle Width="200px" />
                        </asp:BoundField>
                    </Columns>
                        </asp:GridView>
                    <asp:Label ID="Message" runat="server" ForeColor="Red"></asp:Label>

    And in the aspx.vb page I have:

    Code:
     
    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
    
            GridView1.Sort(e.SortExpression, SortDirection.Ascending)
            GridView1.DataBind()
        End Sub
    but this isn't working, when I click in collumn "Nome" the error is:

    An unhandled exception of type 'System.StackOverflowException' occurred in System.Web.dll

    Some help?

    regards,

  2. #2
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: Grid view Sorting

    how/where are you binding the data initially? ie where you assign the gridview's datasource

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  3. #3
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: Grid view Sorting

    the way i perform manual paging and sorting... keep in mind i just pulled this from one of my apps
    vb Code:
    1. Private Property GridViewPageIndex() As Integer
    2.         Get
    3.             If ViewState("GridViewPageIndex") IsNot Nothing Then
    4.                 Return ViewState("GridViewPageIndex")
    5.             End If
    6.             Return 0
    7.         End Get
    8.         Set(ByVal value As Integer)
    9.             ViewState("GridViewPageIndex") = value
    10.         End Set
    11.     End Property
    12.  
    13.     Private Property GridViewSortDirection() As SortDirection
    14.         Get
    15.             If ViewState("sortDirection") Is Nothing Then
    16.                 ViewState("sortDirection") = SortDirection.Ascending
    17.             End If
    18.             Return ViewState("sortDirection")
    19.         End Get
    20.         Set(ByVal value As SortDirection)
    21.             ViewState("sortDirection") = value
    22.         End Set
    23.     End Property
    24.  
    25.     Private Property GridViewSortExpression() As String
    26.         Get
    27.             If String.IsNullOrEmpty(ViewState("sortExpression")) Then
    28.                 ViewState("sortExpression") = DBConstants.COL_HISTORICALTRANSACTIONLOG_ID
    29.             End If
    30.             Return ViewState("sortExpression")
    31.         End Get
    32.         Set(ByVal value As String)
    33.             ViewState("sortExpression") = value
    34.         End Set
    35.     End Property
    36.  
    37.     Private Sub ApplyFilter(ByVal direction As String)
    38.         Dim dt As Ril.HistoricalTransactionLogTable = _
    39.             Ril.TransactionManager.GetHistoricalTransactionLogs(QueryClauseCollectionParameter)
    40.         Dim dv As New Data.DataView(dt)
    41.         dv.Sort = GridViewSortExpression + " " + direction
    42.         gvTransactions.DataSource = dv
    43.         gvTransactions.PageIndex = GridViewPageIndex
    44.         gvTransactions.DataBind()
    45.     End Sub
    46.  
    47.     Protected Sub gvTransactions_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvTransactions.PageIndexChanged
    48.         If GridViewSortDirection = SortDirection.Ascending Then
    49.             ApplyFilter("ASC")
    50.         Else
    51.             ApplyFilter("DESC")
    52.         End If
    53.     End Sub
    54.  
    55.     Protected Sub gvTransactions_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvTransactions.PageIndexChanging
    56.         GridViewPageIndex = e.NewPageIndex
    57.     End Sub
    58.  
    59.     Protected Sub gvTransactions_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvTransactions.Sorting
    60.         GridViewSortExpression = e.SortExpression
    61.     End Sub
    62.  
    63.     Protected Sub gvTransactions_Sorted(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvTransactions.Sorted
    64.         If GridViewSortDirection = SortDirection.Ascending Then
    65.             GridViewSortDirection = SortDirection.Descending
    66.             ApplyFilter("DESC")
    67.         Else
    68.             GridViewSortDirection = SortDirection.Ascending
    69.             ApplyFilter("ASC")
    70.         End If
    71.     End Sub

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  4. #4

    Thread Starter
    Lively Member achor's Avatar
    Join Date
    May 2006
    Location
    Porto
    Posts
    123

    Re: Grid view Sorting

    Hello vbdotnetboy,

    I have an data acess layer:

    Code:
    Dim ServiceAdapter As New GestFacturasTableAdapters.ServicosClienteTableAdapter
    Dim rsSecl As GestFacturas.ServicosClienteDataTable
    
    rsSecl = ServiceAdapter.GetServicosdoCliente(Id_Client)
    GridView1.DataSource = rsSecl
    GridView1.DataBind()
    line2: GestFacturasTableAdapters ... GestFacturas is the name of my database.


    About your code:

    In line 38 what is this DBConstants.COL_HISTORICALTRANSACTIONLOG_ID ?

  5. #5
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: Grid view Sorting

    DBConstants.COL_HISTORICALTRANSACTIONLOG_ID is the way we conform allow for easy change. all this is, is just a class that exposes public constants that are strings with the name of the column in a table or database. we also use it for storing the name of tables, views and stored procedures. this way if anything changes within the database say the name has to be changed then all we have to do is change it once within the DBConstants class and it's in affect throughout the rest of the project, rather then having to search through all the project files to find all occurrences. hope this makes sense


    also did you get it working?

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  6. #6

    Thread Starter
    Lively Member achor's Avatar
    Join Date
    May 2006
    Location
    Porto
    Posts
    123

    Re: Grid view Sorting

    hello!

    yes it worked, I made some changes to the code you gave me.

    Many thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width