Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 8th, 2008, 12:29 PM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default [Resolved] GridView Sorting and Paging

I have a GridView where I do paging and sorting (on multiple columns).

When I initially display the GridView paging works correctly. When I sort on a column it sorts the column correctly. But when I do paging after I have sorted a column it does not display the column in sort order (it looks like it always defaults to asc). Can anyone help please:




Code:
Protected Sub GridView1_PageIndexChanging1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging

        mySqlDataSet = Session("mySqlDataSet")
        GridView1.PageIndex = e.NewPageIndex
        GridView1.DataSource = mySqlDataSet
        GridView1.DataBind()

    End Sub


Private Property GridViewSortDirection() As String

        Get
            Return IIf(ViewState("SortDirection") = Nothing, "DESC", ViewState("SortDirection"))
        End Get
        Set(ByVal value As String)
            ViewState("SortDirection") = value
        End Set

    End Property


    Private Property GridViewSortExpression() As String

        Get
            Return IIf(ViewState("SortExpression") = Nothing, String.Empty, ViewState("SortExpression"))
        End Get
        Set(ByVal value As String)
            ViewState("SortExpression") = value
        End Set

    End Property


    Private Function GetSortDirection() As String

        Select Case GridViewSortDirection
            Case "ASC"
                GridViewSortDirection = "DESC"
            Case "DESC"
                GridViewSortDirection = "ASC"
        End Select
        Return GridViewSortDirection

    End Function


    Protected Function SortDataTable(ByVal dataTable As Data.DataTable, ByVal isPageIndexChanging As Boolean) As Data.DataView

        If Not dataTable Is Nothing Then
            Dim dataView As New Data.DataView(dataTable)
            If GridViewSortExpression <> String.Empty Then
                If isPageIndexChanging Then
                    dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection)
                Else
                    dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GetSortDirection())
                End If
            End If
            Return dataView
        Else
            Return New Data.DataView()
        End If

    End Function


    Protected Sub gridView1_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)

        Session("InitialSort") = "False"
        GridViewSortExpression = e.SortExpression
        mySqlDataSet = Session("mySqlDataSet")
        GridView1.DataSource = mySqlDataSet.Tables("JobListTable")
        GridView1.DataSource = SortDataTable(GridView1.DataSource, False)
        GridView1.DataBind()

    End Sub


    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

        e.Row.Height = Unit.Pixel(20)

    End Sub


    Sub AddSortImage(ByVal sortColumnIndex As Integer, ByVal HeaderRow As GridViewRow)

        Dim sortImage As New Image() 'Create the sorting image based on the sort direction.

        If GridViewSortDirection = "ASC" Then
            sortImage.ImageUrl = "~/Icons/up.gif"
        End If

        If GridViewSortDirection = "DESC" Then
            sortImage.ImageUrl = "~/Icons/down.gif"
        End If

        HeaderRow.Cells(sortColumnIndex).Controls.Add(sortImage) 'Add the image to the appropriate header cell.

    End Sub


    Function GetSortColumnIndex() As Integer

        Dim field As DataControlField 'Iterate through the Columns collection to determine the index of the column being sorted.
        For Each field In GridView1.Columns
            If field.SortExpression = GridViewSortExpression Then
                Return GridView1.Columns.IndexOf(field)
            End If
        Next
        Return -1

    End Function
 
Old October 9th, 2008, 07:45 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Calling function SortDataTable in the page change event solved the problem.






Similar Threads
Thread Thread Starter Forum Replies Last Post
[Resolved] GridView to Excel snufse ASP.NET 2.0 Basics 2 October 28th, 2008 02:37 PM
[Resolved] GridView TemplateField - get value snufse ASP.NET 2.0 Basics 25 July 29th, 2008 07:25 AM
Sorting and Paging of gridview lily611 C# 2005 0 April 24th, 2008 03:09 AM
[Resolved] IListSource problem in GridView snufse ASP.NET 2.0 Basics 5 February 12th, 2008 01:42 PM
Sorting and Paging GridView using Alphabets yukijocelyn ASP.NET 2.0 Basics 9 August 29th, 2007 04:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.