Solved

Where to Call Sorting and Paging methods From with asp:Gridview

Posted on 2007-11-30
1
318 Views
Last Modified: 2013-11-26
Im using the following code in my BindData() function:
I just need to know where I can call my Sorting and Paging methods from with this code:
Maybe Im not understanding this correctly, but, I start off with a DataReader and unload it to a DataTable but then I have to convert to a DataView to Sort the table.
Is there any way around this if Im using an SqlDataSource entirely through code?
I have a rowfilter I want to apply to my dataset but I dont know where before I rebind the gridview.
Thanks
John
Sub BindData(ByVal GetFresh As Boolean, Optional ByVal TextSearch As Boolean = False, Optional ByVal Pageindexchange As Boolean = False)
            Try
                If GetFresh = True And TextSearch = False Then
                    strFilter = "All"
                End If
                Dim myConn2 As SqlConnection = New SqlConnection(ConfigurationManager.AppSettings("connectionString").ToString())
                Dim myComm2 As SqlCommand = New SqlCommand()
                myComm2.Connection = myConn2
                strId = Me.ddlLocation.SelectedValue
                If strId = 0 Or strId = "" Then
                    myComm2.CommandText = "SelectClients"
                Else
                    myComm2.CommandText = "SelectClientsByLocation"
                End If
                myComm2.CommandType = CommandType.StoredProcedure
                myConn2.Open()
                rdr2 = myComm2.ExecuteReader()
                'change datasource from datareader to datatable
                Dim DT As New DataTable()
                'Load Data from Data Reader to DataTable object
                DT.Load(rdr2)
                lblCount.Text = DT.Rows.Count
                lblSort.Text = GridViewSortDirection
                dataTableRowCount = DT.Rows.Count
 
                If dataTableRowCount > 0 Then
                    gvAddress.DataSource = DT
                    gvAddress.DataBind()
                End If
 
                'Dim ds1 As DataSet = Nothing
                Dim nDT As DataView
                'ds1 = convertDataReaderToDataSet(rdr2)
                'nDT = ds1.Tables(0).DefaultView
 
                nDT = SortDataTable(DT, Pageindexchange)
                lblDir.Text = nDT.Sort
 
                If strId = CStr(0) Or strId = "" Then  ' 0 for all locations
                    If strFilter = "All" Then
                        nDT.RowFilter = String.Empty
                    Else
                        nDT.RowFilter = "ContactLastName LIKE '" & Trim(strFilter) & "%' "
                    End If
                Else
                    If strFilter = "All" Then
                        nDT.RowFilter = "LocationID=" & Trim(strId)
                    Else
                        nDT.RowFilter = "ContactLastName LIKE '" & Trim(strFilter) & "%'  and LocationID=" & Trim(strId)
                    End If
                End If
--------------------------------------------------
--generic sorting and paging routines:
        Protected Function SortDataTable(ByVal dataTable As DataTable, ByVal isPageIndexChanging As Boolean) As DataView
            If Not dataTable Is Nothing Then
 
                Dim dataView As DataView = New DataView(dataTable)
                If GridViewSortExpression <> String.Empty Then
                    If isPageIndexChanging Then
                        'dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection)
                        dataView.Sort = GridViewSortExpression + " " + GridViewSortDirection
                    Else
                        dataView.Sort = GridViewSortExpression + " " + GetSortDirection()
 
                        'dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GetSortDirection())
                    End If
                End If
                Return dataView
            Else
                Return New DataView()
            End If
        End Function
---------------------------------------------------
        Private Function GetSortDirection() As String
            Select Case GridViewSortDirection
                Case "ASC"
                    GridViewSortDirection = "DESC"
                Case "DESC"
                    GridViewSortDirection = "ASC"
            End Select
            Return GridViewSortDirection
        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

Open in new window

0
Comment
Question by:jtrapat1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 10

Accepted Solution

by:
Alpesh Patel earned 250 total points
ID: 20384844
Yes you can use that

now regarding sorting and paging its long to solve your code. but i can give u solutions which u can apply for your problem

your VB page has this scenario

protected sub grid_DataBind(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grd.PageIndexChanging

bind your data here with either way no matter wheather it is with Datareader or with SqlDatasource

end sub

 Protected Sub grd_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grd.PageIndexChanging

        grd.PageIndex = e.NewPageIndex
apply databinding here
    End Sub




 Protected Sub grd_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grd.Sorting
apply DataBinding here with  column name   e.SortExpression.ToString
End Sub




Your ASPX page has this scenario

1.  make your grid attrribute for paging= true and for sorting=true
2. for each column set attribute SortExpression='YourColumnName'

thats it
it would definetly work
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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