0

我的应用程序出现问题,我在填充 DataGridView 控件时随机收到错误“单元格的格式化值的类型错误”。

此控件绑定到一个数据表,该数据表从我的数据库中构建行列表。我有单元格格式,可以检查单元格数据并相应地更改它。例如,如果 cell.value = 'x' then cell.value = y 等。这似乎在大约 99% 的时间内都可以正常工作,但我时不时会收到上述错误。如果我清除错误并刷新,DataGridView 控件会很好地填充。

无论如何,我可以准确找出导致此错误的原因吗?我的代码看起来像这样......

Private Sub dgvRegisters_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgvRegisters.CellFormatting

Try
    If e.RowIndex < 0 Then Exit Sub

    If (dgvRegisters.Columns(e.ColumnIndex).Name = "r_Online") Then
        e.FormattingApplied = True

        Select Case Convert.ToInt32(dgvRegisters.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
            Case 1
                e.Value = ilstVNC.Images(0)
            Case Else
                e.Value = ilstVNC.Images(1)
        End Select
        If XMLLoggingType = "Debug (All Activity)" Then CreateLog("Formatting DatagridView('Registers')  " & dgvRegisters.Columns(e.ColumnIndex).Name & "(" & e.RowIndex + 1 & ")")
    End If

    If (dgvRegisters.Columns(e.ColumnIndex).Name = "r_OS") Then
        e.FormattingApplied = True

        Select Case (dgvRegisters.Rows(e.RowIndex).Cells(e.ColumnIndex).Value).ToString
            Case "Microsoft Windows XP Professional"
                e.Value = "Win XP"
            Case Else
                e.Value = ""
        End Select
        If XMLLoggingType = "Debug (All Activity)" Then CreateLog("Formatting DatagridView('Registers')  " & dgvRegisters.Columns(e.ColumnIndex).Name & "(" & e.RowIndex + 1 & ")")
    End If

    If (dgvRegisters.Columns(e.ColumnIndex).Name = "r_TimeZone") Then
        e.FormattingApplied = True

        Select Case (dgvRegisters.Rows(e.RowIndex).Cells(e.ColumnIndex).Value).ToString
            Case "(GMT+09:30) Adelaide"
                e.Value = "Adelaide"
            Case "(GMT+10:00) Brisbane"
                e.Value = "Brisbane"
            Case "N/A"
                e.Value = ""
            Case Else
                e.Value = ""
        End Select
        If XMLLoggingType = "Debug (All Activity)" Then CreateLog("Formatting DatagridView('Registers')  " & dgvRegisters.Columns(e.ColumnIndex).Name & "(" & e.RowIndex + 1 & ")")
    End If

    If (dgvRegisters.Columns(e.ColumnIndex).Name = "r_ComputerType") Then
        e.FormattingApplied = True

        Select Case (dgvRegisters.Rows(e.RowIndex).Cells(e.ColumnIndex).Value).ToString
            Case "AWRDACPI"
                e.Value = "A-Box 122"
            Case "HP Compaq dx7400 SFF", "HP Compaq dx7400 Small Form Factor.", "HP Compaq dx7400 Microtower"
                e.Value = "HP DX7400"
            Case "GHD385AV"
                e.Value = "HP GHD385"
            Case "To Be Filled By O.E.M."
                e.Value = "A-Box 120"
            Case Else
                e.Value = "Unknown"
        End Select
        If XMLLoggingType = "Debug (All Activity)" Then CreateLog("Formatting DatagridView('Registers')  " & dgvRegisters.Columns(e.ColumnIndex).Name & "(" & e.RowIndex + 1 & ")")
    End If

    If (dgvRegisters.Columns(e.ColumnIndex).Name = "r_p_pos") Or (dgvRegisters.Columns(e.ColumnIndex).Name = "r_p_updprg") Or (dgvRegisters.Columns(e.ColumnIndex).Name = "r_p_communic") Or (dgvRegisters.Columns(e.ColumnIndex).Name = "r_p_eftclt") Or (dgvRegisters.Columns(e.ColumnIndex).Name = "r_p_eftsvr") Then
        e.FormattingApplied = True

        Select Case Convert.ToInt32(dgvRegisters.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
            Case 1
                e.Value = ilstStatus.Images(0)
            Case Else
                e.Value = ilstStatus.Images(1)
        End Select
        If XMLLoggingType = "Debug (All Activity)" Then CreateLog("Formatting DatagridView('Registers')  " & dgvRegisters.Columns(e.ColumnIndex).Name & "(" & e.RowIndex + 1 & ")")
    End If

Catch ex As Exception
    If (XMLLogErrors = True And XMLLoggingType = "Custom") Or XMLLoggingType = "Debug (All Activity)" Then CreateLog(ex.Message)
    If MySQLConn.State = ConnectionState.Open Then MySQLConn.Close()

End Try
End Sub

基本上,这段代码只是运行并检查特定单元格的特定值。我对 vb.net 相当陌生,但如果我的代码有问题,那么我希望每次加载 DataGridView 控件时都会收到错误消息。

任何帮助将不胜感激。

4

1 回答 1

0

好的,在玩弄我的代码时,我想我找到了问题的原因,但没有找到答案。

我认为问题是当我的 SQL 查询未能返回任何结果(或未能足够快地返回结果)时,我的代码进入 Cell_Formatting 事件但由于没有数据而出错:

Private Sub dgvRegisters_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgvRegisters.CellFormatting Try If e.RowIndex < 0 Then Exit Sub

    If (dgvRegisters.Columns(e.ColumnIndex).Name = "r_Online") Then
        e.FormattingApplied = True

        Select Case Convert.ToInt32(dgvRegisters.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) 'Code falls over here with Object reference not set to an instance of an object

如果数据集返回零记录,我不知道如何防止我的代码进入格式化事件,或者更好的是,如果没有要格式化的记录,则跳过格式化。

于 2013-08-28T07:16:00.767 回答