我的应用程序出现问题,我在填充DataGridView控件时随机收到错误“单元格的格式化值类型错误”。
此控件绑定到一个数据表,该数据表构建了我的数据库中的行列表。我有单元格格式,检查单元格数据并相应地更改它。例如,如果cell.value ='x',则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控件时我都会收到错误。
感谢任何帮助。
答案 0 :(得分:0)
好的,在玩我的代码时,我认为我找到了问题的原因,但不是答案。
我认为问题是当我的SQL查询无法返回任何结果(或无法足够快地返回结果)时,我的代码进入Cell_Formatting事件但由于没有数据而导致错误:
Private Sub dgvRegisters_CellFormatting(sender As Object,e As DataGridViewCellFormattingEventArgs)处理dgvRegisters.CellFormatting 尝试 如果e.RowIndex&lt; 0然后退出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
如果数据集返回零记录,我不知道如何阻止我的代码进入格式化事件,或者更好的是,如果没有要格式化的记录,则跳过格式化。