jtrapat1
asked on
Where to Call Sorting and Paging methods From with asp:Gridview
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.