Sorting Ascending and Descending
Toggling the direction of your datagrid sort is easy using a session to remember which direction you sorted last.
You will want to declare an empty string in the top of your page
Dim SortField As String
SortField = e.SortExpression If Session("toggle") = True Then
SortField += " DESC"
Session("toggle") = False
Else
SortField += " Asc"
Session("toggle") = True
End If Dim orderstring As String = SortField
orderbyString = " order by " & orderby
Else
orderbyString = " order by 2,3,10 "
End If
sSelect = sSelect & orderbyString
Then on your sort command add this logic - Example of entire command
Private Sub dgTaskExceptions_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgTaskExceptions.SortCommandSortField = e.SortExpression If Session("toggle") = True Then
SortField += " DESC"
Session("toggle") = False
Else
SortField += " Asc"
Session("toggle") = True
End If Dim orderstring As String = SortField
'REBIND
dgTaskExceptions.DataSource = POTracking_Data.tracking.getSampleTasks(orderstring)
dgTaskExceptions.DataBind()
In my datalayer I add this logic to my query and pass in an OPTIONAL value
Dim orderbyString As String = String.Empty If orderby <> "0" ThenorderbyString = " order by " & orderby
Else
orderbyString = " order by 2,3,10 "
End If
sSelect = sSelect & orderbyString
Don't forget to add the sortexpression to your boundcolumns e.g.
<
asp:BoundColumn dataField="PO" headertext="PO" sortExpression="2"></asp:BoundColumn>
Comments