dOOdad, Sorting in an ASP datagrid made easy
I have been playing around with the dOOdad architecture and found out something very nice about this architecture.
Private Sub dgQuestions_SortCommand(Bla bla..) Handles dgQuestions.SortCommand
' Let's get the Questions from the cache
MyQuestions = Cache.Get("MyQuestions")
' Determine some sort orders
Dim OldSortOrder As String = MyQuestions.Sort
Dim NewSortOrderD As String = e.SortExpression + " DESC"
Dim NewSortOrderA As String = e.SortExpression + " ASC"
' See if the sort is on a new column
If OldSortOrder <> NewSortOrderA And OldSortOrder <> NewSortOrderD Then
' Sort is on a new column
MyQuestions.Sort = NewSortOrderD
Else
' it's on the same column, reverse the sort
If OldSortOrder = NewSortOrderA Then
MyQuestions.Sort = NewSortOrderD
Else
MyQuestions.Sort = NewSortOrderA
End If
End If
'Rewind the Questions
MyQuestions.Rewind()
' Remove old instance from cache and add the Sorted one
Cache.Remove("MyQuestions")
Cache.Insert("MyQuestions", MyQuestions)
'Bind the Grid
BindGrid()
End Sub
The nice part about it is that this piece of code wil look the same for every datagrid. Only the BusinessClass will vary. And presto we have sorting on every column, Ascending and descending !.