DatagridView Randomly Gets Formatting Error

506 Views Asked by At

I am having issues with my application where I randomly receive an error "Formatted value of the cell has a wrong type" when populating my DataGridView control.

This control is bound to a datatable which builds a list of rows from my database. I have cell formatting in place which checks for cell data and changes it accordingly. For example, if cell.value = 'x' then cell.value = y, etc. This seems to work fine for about 99% of the time, but every now and then I receive the above error. If I clear the error and refresh, the DataGridView control populates fine.

Is there anyway I can track down exactly what is causing this error? My code looks like so...

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

Basically this code just runs through and checks specific cells for specific values. I'm reasonably new to vb.net, but if my code was faulty then I would expect to receive the error everytime I load the DataGridView control.

Any help would be appreciated thanks.

1

There are 1 best solutions below

0
On

Okay, in playing around with my code, I think I have found the cause of the problem, but not the answer.

I think the problem is when my SQL query fails to return any results (or fails to return results quick enough), my code enters into the Cell_Formatting event but errors due to no data:

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

I don't know how to prevent my code from going in to the formatting event if the dataset returns zero records, or better still, skip over the formatting if there are no records to format.