[英]How to sort datagridview column with positive and negative numbers?
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?告诉我如何对 datagridview 进行排序,以便首先有 0 和更高的正值,然后是负值?
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.