Wanted to introduce ASC/DESC sorting in a DataGrid using VB (found 10 or so C# routines on the net). The two I did find both failed.
The first routine, below, allows the 0 grid colum to sort both ascending and descending on consecutive cicks but only allows the remaining rows to sort ascending (code from 4GuysFromRolla.com). Any ideas on what may be going wrong?
Sub Sort_Grid(ByVal Sender As Object, ByVal e As DataGridSortCommandEventArgs)
Dim SortExprs() As String
Dim CurrentSearchMode As String, NewSearchMode As String
Dim ColumnToSort As String, NewSortExpr as String
' Parse the sort expression - delimiter space
SortExprs = Split(e.SortExpression, " "
ColumnToSort = SortExprs(0)
' If a sort order is specified get it, else default is descending
If SortExprs.Length() > 1 Then
CurrentSearchMode = SortExprs(1).ToUpper()
If CurrentSearchMode = "ASC" Then
NewSearchMode = "Desc"
Else
NewSearchMode = "Asc"
End If
Else ' If no mode specified, Default is descending
NewSearchMode = "ASC"
End If
' Derive the new sort expression.
NewSortExpr = ColumnToSort & " " & NewSearchMode
' Figure out the column index
Dim iIndex As Integer
Select Case ColumnToSort.toUpper()
case "AwwSiteCode"
iIndex = 0
case "Group_Name"
iIndex = 1
case "Waterbody_Name"
iIndex = 2
case "Description"
iIndex = 3
case "LastDate"
iIndex = 6
End Select
' alter the column's sort expression
dgGroups.Columns(iIndex).SortExpression = NewSortExpr
' Sort the data in new order
GetSites(NewSortExpr)
End Sub
...GetSites(sortField As String)is working properly.
**************
A second routine (FYI - lost carrying out the same functionaity (tested once):
Private Sub dgGroups_SortCommand(S As Object, e As DataGridSortCommandEventArgs)
Dim sOrder As String = dgGroups.Attributes("ORDER"
Dim sExp As String = dgGroups.Attributes("EXPRESSION"
If (sExp <> e.SortExpression) Then
sOrder = "ASC"
sExp = e.SortExpression
Else
sOrder = IIf(sOrder = "ASC", "DESC", "ASC"
End If
Dim oDV As DataView 'failed here on 1st test
oDV.Sort = sExp & " " & sOrder
dgGroups.Attributes.Add("ORDER", sOrder)
dgGroups.Attributes.Add("EXPRESSION", sExp)
End Sub
....of if anyone out there has a VB routine that accomplishes the same. I suppose the former might be the best approach.
The first routine, below, allows the 0 grid colum to sort both ascending and descending on consecutive cicks but only allows the remaining rows to sort ascending (code from 4GuysFromRolla.com). Any ideas on what may be going wrong?
Sub Sort_Grid(ByVal Sender As Object, ByVal e As DataGridSortCommandEventArgs)
Dim SortExprs() As String
Dim CurrentSearchMode As String, NewSearchMode As String
Dim ColumnToSort As String, NewSortExpr as String
' Parse the sort expression - delimiter space
SortExprs = Split(e.SortExpression, " "
ColumnToSort = SortExprs(0)
' If a sort order is specified get it, else default is descending
If SortExprs.Length() > 1 Then
CurrentSearchMode = SortExprs(1).ToUpper()
If CurrentSearchMode = "ASC" Then
NewSearchMode = "Desc"
Else
NewSearchMode = "Asc"
End If
Else ' If no mode specified, Default is descending
NewSearchMode = "ASC"
End If
' Derive the new sort expression.
NewSortExpr = ColumnToSort & " " & NewSearchMode
' Figure out the column index
Dim iIndex As Integer
Select Case ColumnToSort.toUpper()
case "AwwSiteCode"
iIndex = 0
case "Group_Name"
iIndex = 1
case "Waterbody_Name"
iIndex = 2
case "Description"
iIndex = 3
case "LastDate"
iIndex = 6
End Select
' alter the column's sort expression
dgGroups.Columns(iIndex).SortExpression = NewSortExpr
' Sort the data in new order
GetSites(NewSortExpr)
End Sub
...GetSites(sortField As String)is working properly.
**************
A second routine (FYI - lost carrying out the same functionaity (tested once):
Private Sub dgGroups_SortCommand(S As Object, e As DataGridSortCommandEventArgs)
Dim sOrder As String = dgGroups.Attributes("ORDER"
Dim sExp As String = dgGroups.Attributes("EXPRESSION"
If (sExp <> e.SortExpression) Then
sOrder = "ASC"
sExp = e.SortExpression
Else
sOrder = IIf(sOrder = "ASC", "DESC", "ASC"
End If
Dim oDV As DataView 'failed here on 1st test
oDV.Sort = sExp & " " & sOrder
dgGroups.Attributes.Add("ORDER", sOrder)
dgGroups.Attributes.Add("EXPRESSION", sExp)
End Sub
....of if anyone out there has a VB routine that accomplishes the same. I suppose the former might be the best approach.