8oris
8oris

Reputation: 463

System.FormatException when populating a DataGridViewComboBoxColumn from SQL Query

Context:

I have a datagridview with one combobox columns called Media. This combobox got two possible values: TV and RADIO. I populate this column with datas coming from SQL Query where the SQL field queried got two possible values: TV and RADIO

Issue: I got an "System.FormatException: The value of DataGridViewComboBoxCell is not valid" on dtgDocEcriteTable.Rows(intDtDocDiffuseurs).Cells("MEDIA").Value = dsUnupdatedData.Tables(0).Rows(intDtDocDiffuseurs)("MEDIA").ToString

Code:

Private Enum structureMediaType
    TV
    RADIO
End Enum
Private Sub frmDocEcriteTable_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim strSelectSqlDocEcriteData As String
    Dim daDocEcriteData As New MySqlDataAdapter
    Dim dtDocDiffuseurs As New DataTable
    Dim intDtDocDiffuseurs As Integer
    Dim colMedia As New DataGridViewComboBoxColumn With {.DataPropertyName = "MEDIA", .HeaderText = "Média", .Name = "MEDIA", .DataSource = lstComboboxMediaSource, .DisplayMember = "Name", .ValueMember = "Value"}
    Dim lstComboboxMediaSource = [Enum].GetNames(GetType(structureMediaType)).[Select](Function(x) New With {Key .Name = x, Key .Value = CInt([Enum].Parse(GetType(structureMediaType), x))}).ToList()
    Dim strMsgException As String = "Erreur dans le Module frmDocEcriteTable_Load" & vbCrLf & strMsgErrorTellYourAdmin & vbCrLf & vbCrLf & "-- Message d'erreur: "

    Try
        With dtgDocEcriteTable.Columns
            .Add(colMedia)          
        End With

        strSelectSqlDocEcriteData = "SELECT MEDIA FROM DOCUMENTS_DIFFUSEURS_TEST"
        daUpdateData = New MySqlDataAdapter(strSelectSqlDocEcriteData, OpenDlwebDocBddConnexion)
        daUpdateData.Fill(dsUnupdatedData)

        For intDtDocDiffuseurs = 0 To dsUnupdatedData.Tables(0).Rows.Count - 1
            dtgDocEcriteTable.Rows.Add()
            dtgDocEcriteTable.Rows(intDtDocDiffuseurs).Cells("MEDIA").Value = dsUnupdatedData.Tables(0).Rows(intDtDocDiffuseurs)("MEDIA").ToString          
        Next
        
        With Me.dtgDocEcriteTable
            .Refresh()
        End With

    Catch ex As Exception
        MsgBox(strMsgException & vbCrLf & "Erreur:  " & ex.Message, MsgBoxStyle.Critical, strMsgBoxDocEcriteTitleError)
        blnStopSub = True
        Me.Dispose()
        Exit Try

    Finally
        CloseDlwebDocConnexion()

    End Try

End Sub

Private Sub DataGridView1_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles dtgDocEcriteTable.DataError
    Dim view As DataGridView = CType(sender, DataGridView)
    view.Rows(e.RowIndex).Cells(e.ColumnIndex).ErrorText = "an error"
    e.ThrowException = False
End Sub

Additionnal Infos: I tried to print dsUnupdatedData.Tables(0).Rows(intDtDocDiffuseurs)("MEDIA").ToString and it's equal to TV or RADIO, so i don't get where the exception came from. Last info: the datagridview column is correctly filled but there is a red cross next to the value:

enter image description here

Upvotes: 1

Views: 98

Answers (0)

Related Questions