Gridview CustomData sorting and paging

In page load event bind the Datasource to your Grid view

 Private Sub PopulatePublishersGridView()

 

        Dim connectionString As String = AccessConnectionString()

        Dim accessConnection As OleDbConnection = New OleDbConnection(connectionString)

 

        Dim sqlQuery As String = “SELECT [PubID], [Name], [Company Name], [Address], [City], [State], [Zip], [Telephone], [Fax], [Comments] FROM Publishers ORDER BY [Name] ASC;”

 

        Dim accessCommand As New OleDbCommand(sqlQuery, accessConnection)

 

        Dim publishersDataAdapter As New OleDbDataAdapter(accessCommand)

        Dim publishersDataTable As New DataTable(“Publishers”)

        publishersDataAdapter.Fill(publishersDataTable)

 

        Dim dataTableRowCount As Integer = publishersDataTable.Rows.Count

 

        If dataTableRowCount > 0 Then

            gridViewPublishers.DataSource = publishersDataTable

            gridViewPublishers.DataBind()

        End If

    End Sub

 

    Private Function AccessConnectionString() As String

        Dim accessDatabasePath As String = Server.MapPath(“~/App_Data/biblio.mdb”)

        Return String.Format(“Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};”, accessDatabasePath)

    End Function

 

    Private Property GridViewSortDirection() As String

        Get

            Return IIf(ViewState(“SortDirection”) = Nothing, “ASC”, 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 Sub gridViewPublishers_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)

        gridViewPublishers.DataSource = SortDataTable(gridViewPublishers.DataSource, True)

        gridViewPublishers.PageIndex = e.NewPageIndex

        gridViewPublishers.DataBind()

    End Sub

 

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

        If Not dataTable Is Nothing Then

            Dim dataView As New 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 DataView()

        End If

    End Function

 

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

        GridViewSortExpression = e.SortExpression

        Dim pageIndex As Integer = gridViewPublishers.PageIndex

        gridViewPublishers.DataSource = SortDataTable(gridViewPublishers.DataSource, False)

        gridViewPublishers.DataBind()

        gridViewPublishers.PageIndex = pageIndex

    End Sub

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        PopulatePublishersGridView()

    End Sub

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s