Can't Figure Out How To Sort On Bind

P

pbd22

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).LastIndexOf("__"),
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
 
F

Fernando Rodriguez, MCP

You need to get a book about coding basics.

The sort order is not being passed as an argument, it is being concatenated
with the string "searchCache" to create a key for it, so the cache should
countain a copy of the ArrayList with the requested sorting, you're missing
the piece of code where the ArrayList is actually sorted and stored in the
cache.

By the way, storing different copies of the same data but with different
sorting is a very bad practice. I would just store one copy and then sort it
before binding by using the ArrayList's Sort method.

HTH,
 
A

Andrew

Why don't u try to sort the arraylist before binding ?
using something like this :

public class MySearchResults
{
public MySearchResults(string aa, string bb, string cc) { }

public class Comparer : System.Collections.IComparer
{
public int Compare(object x, object y)
{
MySearchResultsx1 = x as MySearchResults;
MySearchResultsy1 = y as MySearchResults;

if (x1 == null || y1 == null)
throw new ArgumentException("Wrong type");
else
return string.Compare(x1.aa, y1.bb);
}
}

cheers
Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top