繁体   English   中英

VB .Net IComparer排序字符串,包括空格数字

[英]VB .Net IComparer sorting string that include blanks numerically

我有一个datagridview数据绑定字符串列,其中包含字符串编号0-99和空白,没有。

我希望在数字上以0到99对该列进行排序,并且在开头或结尾处都是空白的单元格。

我使用DataGridView_ColumnHeaderMouseClick事件并检查列索引以获取正确的索引,然后使用IComparer进行排序。

Class Colomn5Comparer : Implements IComparer
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        Dim XX As DataGridViewRow = DirectCast(x, DataGridViewRow)
        Dim YY As DataGridViewRow = DirectCast(y, DataGridViewRow)
        If Not XX.Cells(5).Value = "" AndAlso Not YY.Cells(5).Value = "" Then

            If CInt(XX.Cells(5).Value) < CInt(YY.Cells(5).Value) Then

                If m_SortOrder = "Desc" Then
                    Return 1
                Else
                    Return -1
                End If

            ElseIf CInt(XX.Cells(5).Value) > CInt(YY.Cells(5).Value) Then

                If m_SortOrder = "Desc" Then
                    Return -1
                Else
                    Return 1
                End If

            Else
                Return 0
            End If

        Else
            Return 0
        End If

    End Function
End Class

我不得不排除没有空白值的单元格,以防止误差大于或小于整数

 If Not XX.Cells(5).Value = "" AndAlso Not YY.Cells(5).Value = "" Then   

这当然会在排序后在当前行位置留下空白的单元格。

有没有办法可以在排序中以某种方式包含空白单元格,以便它们出现在排序行的底部或顶部?

刚刚找到答案,当排除空值时,如果我返回-1,它们将排序到开头。

一些变化会给你更多

If XX.Cells(5).Value = "" AndAlso  YY.Cells(5).Value = "" Then return 0;
If XX.Cells(5).Value = ""  Then return -1;
If YY.Cells(5).Value = ""  Then return +1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM