Gridview Paging/Sorting

Views: This article has been read 92335 times.
Abstract: GridViews that do not use the SqlDataSource control require special subroutines created to handle the Gridview's paging and sorting features. The SqlDataSource control handles the GridView’s paging and sorting of its data “behind the scenes”. This tutorial describes how to create these special subroutines when the SqlDataSource control is not used with the GridView.

Gridview Paging/Sorting

When the user selects a page index displayed in a GridView (represented as a number either in the GridView’s header, footer or both), the “SortDataTable” function (described below) is called to handle the paging or displaying of the next set of records. In this example, 10 records are displayed at one time in the GridView. This function is also called when the user selects one of the GridView’s column headings. These column headings are hyperlinked in order to sort the data in alphanumeric order based on the column data selected. Ascending and descending order are toggled through the hyperlink.

The following code is used to call the “SortDataTable” function. Two parameters are passed to this function: the DataTable that was previously stored in a session variable after initially returning a result set from a database stored procedure (containing the first 100 records), and either a “True” or “False” boolean in the second parameter specifying to the function whether to sort the data based on paging or alphanumerically.

     ‘parameter set to True when paging data
     GridView1.DataSource = SortDataTable(Session("DataTable"), True)

     ‘parameter set to False when sorting data alphanumerically
     GridView1.DataSource = SortDataTable(Session("DataTable"), False)

The paging and sorting subroutines are specified in the <asp:GridView> element. For example, the following code specifies the”GridView1_PageIndexChanging” and ”GridView1_Sorting” subroutines.

     <asp:GridView ID="GridView1"
                              runat="server"
                              Width="100%"
                              AllowPaging="True"
                              AllowSorting="True"
                              PageSize="10"
                              OnPageIndexChanging="GridView1_PageIndexChanging"
                              OnSorting="GridView1_Sorting"
                              AutoGenerateColumns="False"
                              PagerSettings-Mode="NumericFirstLast"              
                              CssClass="GridViewMaster"
                              AlternatingRowStyle-BackColor="#edf3fe"
                              PagerSettings-Position="TopAndBottom"
                              PagerStyle-HorizontalAlign="Left"
                              PagerStyle-VerticalAlign="Middle"
                              HeaderStyle-HorizontalAlign="Center"
                              HeaderStyle-VerticalAlign="Top"
                              HeaderStyle-Height="20px">

The following displays the “SortDataTable” function. This function is used to sort the data in the GridView when the user either pages or sorts alphanumerically through the GridView’s controls.

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

        If Not dataTable Is Nothing Then
            Response.Write("<br>dataTable something: " + System.DateTime.Now)
            Dim dataView As DataView = New DataView(dataTable)
           
        If GridViewSortExpression <> String.Empty Then
                Response.Write("<br>GridViewSortExpression: " + GridViewSortExpression + System.DateTime.Now)

                If isPageIndexChanging Then
                    dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection)
                    Response.Write("<br>isPageIndexChanging in if: " + System.DateTime.Now)
                Else
                    dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GetSortDirection())
                    Response.Write("<br>isPageIndexChanging in else: " + System.DateTime.Now)
                End If
            End If

            Return dataView
        Else
            Response.Write("<br>dataTable nothing: " + System.DateTime.Now)
            Return New DataView()
        End If

    End Function

By Todd Paholsky