Cleaned up column sorting for cross-platform
This commit is contained in:
+41
-33
@@ -154,39 +154,27 @@ Public Class frmSessions
|
|||||||
dgSessions.Columns(iHoursCol).HeaderCell.SortGlyphDirection = SortOrder.None
|
dgSessions.Columns(iHoursCol).HeaderCell.SortGlyphDirection = SortOrder.None
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Private Function GetSortOrder(ByVal bToggle As Boolean, ByVal iCol As Integer) As ListSortDirection
|
Private Sub DoSort(ByRef bToggle As Boolean, ByVal iCol As Integer, ByVal iType As RowCompareHelper.iDataType)
|
||||||
Dim oSortType As ListSortDirection
|
bToggle = Not bToggle
|
||||||
|
|
||||||
If bToggle Then
|
If bToggle Then
|
||||||
oSortType = ListSortDirection.Ascending
|
dgSessions.Sort(New RowCompareHelper(SortOrder.Ascending, iCol, iType))
|
||||||
dgSessions.Columns(iCol).HeaderCell.SortGlyphDirection = SortOrder.Ascending
|
dgSessions.Columns(iCol).HeaderCell.SortGlyphDirection = SortOrder.Ascending
|
||||||
Else
|
Else
|
||||||
oSortType = ListSortDirection.Descending
|
dgSessions.Sort(New RowCompareHelper(SortOrder.Descending, iCol, iType))
|
||||||
dgSessions.Columns(iCol).HeaderCell.SortGlyphDirection = SortOrder.Descending
|
dgSessions.Columns(iCol).HeaderCell.SortGlyphDirection = SortOrder.Descending
|
||||||
End If
|
End If
|
||||||
|
End Sub
|
||||||
Return oSortType
|
|
||||||
End Function
|
|
||||||
|
|
||||||
Private Sub HandleSort(ByVal iCol As Integer)
|
Private Sub HandleSort(ByVal iCol As Integer)
|
||||||
ClearManualSortGlyphs()
|
ClearManualSortGlyphs()
|
||||||
|
|
||||||
Select Case iCol
|
Select Case iCol
|
||||||
Case iStartDisplayCol
|
Case iStartDisplayCol
|
||||||
bStartSortAsc = Not bStartSortAsc
|
DoSort(bStartSortAsc, iStartDisplayCol, RowCompareHelper.iDataType.DateTimeType)
|
||||||
dgSessions.Sort(dgSessions.Columns(iStartDataCol), GetSortOrder(bStartSortAsc, iCol))
|
|
||||||
Case iEndDisplayCol
|
Case iEndDisplayCol
|
||||||
bEndSortAsc = Not bEndSortAsc
|
DoSort(bEndSortAsc, iEndDisplayCol, RowCompareHelper.iDataType.DateTimeType)
|
||||||
dgSessions.Sort(dgSessions.Columns(iEndDataCol), GetSortOrder(bEndSortAsc, iCol))
|
|
||||||
Case iHoursCol
|
Case iHoursCol
|
||||||
bHoursSortAsc = Not bHoursSortAsc
|
DoSort(bHoursSortAsc, iHoursCol, RowCompareHelper.iDataType.DecimalType)
|
||||||
If bHoursSortAsc Then
|
|
||||||
dgSessions.Sort(New RowComparer(SortOrder.Ascending, iHoursCol))
|
|
||||||
dgSessions.Columns(iHoursCol).HeaderCell.SortGlyphDirection = SortOrder.Ascending
|
|
||||||
Else
|
|
||||||
dgSessions.Sort(New RowComparer(SortOrder.Descending, iHoursCol))
|
|
||||||
dgSessions.Columns(iHoursCol).HeaderCell.SortGlyphDirection = SortOrder.Descending
|
|
||||||
End If
|
|
||||||
End Select
|
End Select
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
@@ -239,31 +227,51 @@ Public Class frmSessions
|
|||||||
HandleSort(e.ColumnIndex)
|
HandleSort(e.ColumnIndex)
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Private Class RowComparer
|
'The Mono version of the DataGridView control automatically treats all data as a string for sorting purposes.
|
||||||
|
'This class manually handles column sorting by data type.
|
||||||
|
Private Class RowCompareHelper
|
||||||
Implements System.Collections.IComparer
|
Implements System.Collections.IComparer
|
||||||
|
|
||||||
Private sortOrderModifier As Integer = 1
|
'We need to manually define data types as the column ValueType doesn't work in Mono either.
|
||||||
Private iSortCol As Integer = 0
|
Public Enum iDataType As Integer
|
||||||
|
StringType = 1
|
||||||
|
DateTimeType = 2
|
||||||
|
IntType = 3
|
||||||
|
DecimalType = 4
|
||||||
|
End Enum
|
||||||
|
|
||||||
Public Sub New(ByVal sortOrder As SortOrder, ByVal iCol As Integer)
|
Private iSortOrderModifier As Integer = 1
|
||||||
|
Private iSortCol As Integer = 0
|
||||||
|
Private iDataTypeCol As iDataType = iDataType.StringType
|
||||||
|
|
||||||
|
Public Sub New(ByVal sortOrder As SortOrder, ByVal iCol As Integer, ByVal iType As iDataType)
|
||||||
iSortCol = iCol
|
iSortCol = iCol
|
||||||
|
iDataTypeCol = iType
|
||||||
|
|
||||||
If sortOrder = SortOrder.Descending Then
|
If sortOrder = SortOrder.Descending Then
|
||||||
sortOrderModifier = -1
|
iSortOrderModifier = -1
|
||||||
ElseIf sortOrder = SortOrder.Ascending Then
|
ElseIf sortOrder = SortOrder.Ascending Then
|
||||||
sortOrderModifier = 1
|
iSortOrderModifier = 1
|
||||||
End If
|
End If
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
|
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
|
||||||
Implements System.Collections.IComparer.Compare
|
Dim iCompareResult As Integer
|
||||||
|
Dim dgRow1 As DataGridViewRow = CType(x, DataGridViewRow)
|
||||||
|
Dim dgRow2 As DataGridViewRow = CType(y, DataGridViewRow)
|
||||||
|
|
||||||
Dim DataGridViewRow1 As DataGridViewRow = CType(x, DataGridViewRow)
|
Select Case iDataTypeCol
|
||||||
Dim DataGridViewRow2 As DataGridViewRow = CType(y, DataGridViewRow)
|
Case iDataType.DecimalType
|
||||||
|
iCompareResult = If(CDec(dgRow1.Cells(iSortCol).Value) < CDec(dgRow2.Cells(iSortCol).Value), -1, 1)
|
||||||
|
Case iDataType.IntType
|
||||||
|
iCompareResult = If(CInt(dgRow1.Cells(iSortCol).Value) < CInt(dgRow2.Cells(iSortCol).Value), -1, 1)
|
||||||
|
Case iDataType.StringType
|
||||||
|
iCompareResult = String.Compare(CStr(dgRow1.Cells(iSortCol).Value), CStr(dgRow2.Cells(iSortCol).Value))
|
||||||
|
Case iDataType.DateTimeType
|
||||||
|
iCompareResult = Date.Compare(CDate(dgRow1.Cells(iSortCol).Value), CDate(dgRow2.Cells(iSortCol).Value))
|
||||||
|
End Select
|
||||||
|
|
||||||
Dim CompareResult As Integer = If(CDec(DataGridViewRow1.Cells(iSortCol).Value) < CDec(DataGridViewRow2.Cells(iSortCol).Value), -1, 1)
|
Return iCompareResult * iSortOrderModifier
|
||||||
|
|
||||||
Return CompareResult * sortOrderModifier
|
|
||||||
End Function
|
End Function
|
||||||
End Class
|
End Class
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user