我试图通过包含DBNull值的列对DataGridView控件进行排序。此DataGridView控件绑定到SQL Server 2012数据库。
当我单击headercell(按升序排序)时,DBNull值将在其余整数值之前排序,以便列中的顶行全部为空,然后是整数值1 ,2,3等按升序排列。
我如何解决这个问题?我宁愿DataGridView控件对顶部带有值的行进行排序,然后是DBNulls。
我试图在空白单元格中插入一个更高的值,然后对它们进行正确排序,但是当我将这些值返回System.DBNull.value时,排序顺序将恢复为上面的方式。
我的代码如下:
If dgv.Columns(e.ColumnIndex).Name.EndsWith("Position") Then
intSortingRunningPosition = 1000
dgv.Sort(baseColumn, System.ComponentModel.ListSortDirection.Ascending)
newColumn.HeaderCell.SortGlyphDirection = Windows.Forms.SortOrder.Ascending
For Each dr As DataGridViewRow In dgv.Rows
If dr.Cells(e.ColumnIndex).Value Is System.DBNull.Value Then
dr.Cells(e.ColumnIndex).Value = intSortingRunningPosition
intSortingRunningPosition += 1
End If
Next
dgv.Sort(newColumn, System.ComponentModel.ListSortDirection.Ascending)
For Each dr As DataGridViewRow In dgv.Rows
If dr.Cells(e.ColumnIndex).Value >= 1000 Then
dr.Cells(e.ColumnIndex).Value = System.DBNull.Value
End If
Next
End If
很抱歉,如果这令人困惑。谢谢!
更新
我修改了我的代码以使用IComparer方法,并提出了以下内容:
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements System.Collections.IComparer.Compare
Dim DataGridViewRow1 As DataGridViewRow = CType(x, DataGridViewRow)
Dim DataGridViewRow2 As DataGridViewRow = CType(y, DataGridViewRow)
Dim CompareResult As Integer
Dim intDGV1Cell0Value As Integer = Nothing
Dim intDGV1Cell1Value As Integer = Nothing
Dim intDGV2Cell0Value As Integer = Nothing
Dim intDGV2Cell1Value As Integer = Nothing
Try
intDGV1Cell0Value = CInt(DataGridViewRow1.Cells(0).Value)
Catch ex As Exception
intDGV1Cell0Value = Int32.MaxValue
End Try
Try
intDGV1Cell1Value = CInt(DataGridViewRow1.Cells(1).Value)
Catch ex As Exception
intDGV1Cell1Value = Int32.MaxValue
End Try
Try
intDGV2Cell0Value = CInt(DataGridViewRow2.Cells(0).Value)
Catch ex As Exception
intDGV2Cell0Value = Int32.MaxValue
End Try
Try
intDGV2Cell1Value = CInt(DataGridViewRow2.Cells(1).Value)
Catch ex As Exception
intDGV2Cell1Value = Int32.MaxValue
End Try
' Try to sort based on the Last Name column.
CompareResult = System.String.Compare(intDGV1Cell1Value, intDGV2Cell1Value)
' If the Last Names are equal, sort based on the First Name.
If CompareResult = 0 Then
CompareResult = System.String.Compare(intDGV1Cell0Value, intDGV2Cell0Value)
End If
Return CompareResult * sortOrderModifier
End Function
空值现在根本没有排序。我在这里做错了什么想法?我确信我有点不在球场,因为它是......
答案 0 :(得分:0)
您可以在名为bit
的数据集中添加NullValue
字段,对于非空的数据列的任何条目,0
都是1
,{{1}}表示{{1}}任何无效的。
然后,作为排序操作的一部分,按数据列本身和此附加位列进行排序,以便空值自然出现在排序中的非空值之后。
答案 1 :(得分:0)
我使用Sort(ICompare)方法对此数据库进行排序是相当不合适的,因为它已绑定到外部数据集。我最终修改了我的连接字符串并重新填充了数据表。
我的代码如下:
If DataType = "Integer" Then
If Column = "Vehicle Number" Then
strConnectionStringCase = "Select * From Timing ORDER BY CAST([Class] AS NVARCHAR(MAX)) " & Direction & ", CONVERT(int, RTRIM(CAST([" & Column & "] AS NVARCHAR(MAX)))) " & Direction
End If
ElseIf DataType = "DateTime" Then
strConnectionStringCase = "Select * From Timing ORDER BY CASE WHEN [" & Column & "] is NULL THEN 1 ELSE 0 END, [" & Column & "] " & Direction
ElseIf DataType = "String" Then
If Column = "Vehicle Number" Then
strConnectionStringCase = "Select * From Timing ORDER BY LEN(CAST([" & Column & "] AS NVARCHAR(MAX))) " & Direction & ", CAST([Class] AS NVARCHAR(MAX)) " & Direction & ", CAST([" & Column & "] AS NVARCHAR(MAX)) " & Direction
End If
End If
我在标题单元格点击事件上调用此子例程。我将列名,排序方向和数据类型发送到此子例程。
此代码还允许我根据数据类型对列进行排序。如果有字母数字字符串,我可以按照类似的顺序对它们进行排序,就好像它们只是整数一样。 DBNull值始终位于排序的末尾。
感谢您的帮助!