Private Sub DataGridViewRez_SortCompare(sender As Object, e As DataGridViewSortCompareEventArgs) Handles DataGridViewRez.SortCompare
Dim num1 As Integer
Dim num2 As Integer
If e.Column.Index = 3 Then
If Integer.TryParse(CStr(e.CellValue1), num1) AndAlso Integer.TryParse(CStr(e.CellValue2), num2) Then
'Order the numbers based on their absolute value.
e.SortResult = Math.Abs(num1).CompareTo(Math.Abs(num2))
Else
' 'At least one of the values is not a number so consider them equivalent for sorting purposes.
e.SortResult = 0
End If
e.Handled = True
End If
End Sub
tell me how can I do the sorting of the datagridview so that first there are positive values from 0 and higher, then negative ones?
I believe that this should do the trick:
If (num1 < 0) = (num2 < 0) Then
'Either both numbers are negative or both are non-negative, so sort on ascending absolute value.
e.SortResult = Math.Abs(num1).CompareTo(Math.Abs(num2))
Else
'One is number is negative and one is non-negative, so sort on descending value
e.SortResult = num2.CompareTo(num1)
End If
If you want it as a one-liner:
e.SortResult = If((num1 < 0) = (num2 < 0), Math.Abs(num1).CompareTo(Math.Abs(num2)), num2.CompareTo(num1))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.