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

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.SortCommand
    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

'REBIND
dgTaskExceptions.DataSource = POTracking_Data.tracking.getSampleTasks(orderstring)
dgTaskExceptions.DataBind()

End Sub

 

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" Then
orderbyString = " 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

Popular posts from this blog

Linq Exclude from separate list

Linq Query Syntax vs Method Syntax