By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

Can't Figure Out How To Sort On Bind

P: n/a

Hi.

I am returning to an old bit of code in our program and need to figure
out how to sort
my columns on bind. I am sorting on Date (mostly) and some other
values.
Problem is, the code is an ArrayList that seems to do some
tricky stuff with a chache object and I am unable to get any sort of
sorting happening.

I have provided the method below (and some supporting methods). The
interesting bit
of code (to me) is:

'Assign ColumnOrder to ViewState
ViewState("SortOrder") = ColumnOrder
'Set up Cache Object and determine if it exists
searchCache = CType(Cache.Get("searchCache" &
ColumnOrder), ArrayList)

It looks like the ArrayList is stored in Cache and then when a new
search parameter is
requested, the cache is called and the new parameter is passed as an
argument?

Any help in explaining how to attack this would be very helpful. I am
not too familiar with
doing the sorting on bind and can't do this in the stored procedure.

Thanks!

-------------------------------------------------------------------------------------------------------------------------------------------------------------

Sub GetDataReader(ByVal ColumnOrder As String)

Try
'Assign ColumnOrder to ViewState
ViewState("SortOrder") = ColumnOrder
'Set up Cache Object and determine if it exists
searchCache = CType(Cache.Get("searchCache" &
ColumnOrder), ArrayList)

'TEMP
searchCache = Nothing

If (searchCache Is Nothing) Then

'Dim sqlStr As String = "call sp_myContent;"

Dim sqlStr As String = "call sp_SearchContent;"

Dim strConn As String =
"SERVER=localhost;DATABASE=MCS;" & _
"UID=vbuser;PASSWORD=vbuser06492;"
Dim MyConnection As New MySqlConnection(strConn)
MyConnection.Open()
Dim MyCommand As New MySqlCommand(sqlStr,
MyConnection)
Dim objDataReader As MySqlDataReader =
MyCommand.ExecuteReader() 'command.close removed
'Create instances of the class,
Dim bkResults As New ArrayList()
'Loop through DataReader
While objDataReader.Read()
With bkResults
If Not objDataReader.IsDBNull(0) Then
.Add(New
MySearchResults(objDataReader.GetString(0).Replace ("<u>/",
"<u>").Remove(objDataReader.GetString(0).LastIndex Of("__"),
objDataReader.GetString(0).Length).ToString(), _

objDataReader.GetString(1).ToString(),
objDataReader.GetString(2).ToString(),
objDataReader.GetString(3).ToString(),
objDataReader.GetString(4).ToString(),
objDataReader.GetString(4).ToString()))
End If
End With
End While

'Insert ArrayList into Cache Object with unique
identifier
Cache.Insert("searchCache" & ColumnOrder,
bkResults)
'Close DataReader Connection
objDataReader.Close()
'Bind DataGrid from ArrayList
MyDataGrid.DataSource = bkResults

Else

'Bind DataGrid from Cached ArrayList
MyDataGrid.DataSource = searchCache

End If

MyDataGrid.DataBind()

'Clear ArrayList
bkResults = Nothing

Catch ex As Exception
myLog.WriteEntry(ex.Message,
EventLogEntryType.Warning)
End Try

End Sub

Sub MyDataGrid_Page(ByVal sender As Object, ByVal e As
DataGridPageChangedEventArgs)
Try
MyDataGrid.CurrentPageIndex = e.NewPageIndex
GetDataReader(ViewState("SortOrder").ToString())
Catch ex As Exception
myLog.WriteEntry(ex.Message,
EventLogEntryType.Warning)
End Try
End Sub
Sub MyDataGrid_Sort(ByVal sender As Object, ByVal e As
DataGridSortCommandEventArgs)
Try
MyDataGrid.CurrentPageIndex = 0

GetDataReader(SortOrder(e.SortExpression.ToString( )))
Catch ex As Exception
myLog.WriteEntry(ex.Message,
EventLogEntryType.Warning)
End Try

End Sub

Function SortOrder(ByVal Field As String) As String

Try
If Field = ViewState("SortOrder").ToString() Then
SortOrder = Replace(Field, "asc", "desc")
Else
SortOrder = Replace(Field, "desc", "asc")
End If

Catch ex As Exception
myLog.WriteEntry(ex.Message,
EventLogEntryType.Warning)
End Try

End Function
Jun 27 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.