1

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

8oris
  • 320
  • 2
  • 12
  • 2
    `ValueMember` is an integer, not a string -- Define the list of values to use as datasource before your set the property. You can also set the datasource of your DGV and cut out a lot of code. -- Not clear why you have all those `with` / `end with` when you call a single method. – Jimi Jan 21 '22 at 12:57
  • 1
    As Jimi notes… You state that _”I tried to print dsUnupdatedData.Tables(0).Rows(intDtDocDiffuseurs)("MEDIA").ToString and it's equal to TV or RADIO, … “_ ? But, according to the combo box column definition, it’s `ValueMember` is an `int` named "Value". Therefore the values in the grid’s data source need be `int` values. 0 for “TV” and 1 for “RADIO.” This would certainly explain the error. – JohnG Jan 21 '22 at 13:00
  • Thanks...but regarding the microsoft documentation, ValueMember seems to be a string., isn't it? https://learn.microsoft.com/fr-fr/dotnet/api/system.windows.forms.datagridviewcomboboxcell.valuemember?view=windowsdesktop-6.0 ```private void SetAlternateChoicesUsingDataSource(DataGridViewComboBoxColumn comboboxColumn) { { comboboxColumn.DataSource = RetrieveAlternativeTitles(); comboboxColumn.ValueMember = ColumnName.TitleOfCourtesy.ToString(); comboboxColumn.DisplayMember = comboboxColumn.ValueMember; } }``` – 8oris Jan 21 '22 at 13:26
  • Deleting the Value Member property from the Dim colMedia line did the job. – 8oris Jan 21 '22 at 13:45
  • 1
    It is true that the combo boxes “ValueMember” property is a `string` type value as it simply “identifies” which column in the data source to use. If you use the `ValueMember` then the "identified" column type in the combo box... and the column type in the grid’s combo box cells must match. As you note you do not have to use it. – JohnG Jan 21 '22 at 13:47
  • 1
    *ValueMember seems to be a string* - yes, it's so you can set to to the **name** of the property that holds the value. Class Person with integer Age property, would have a combo ValueMember of "Age". Class Order with string DeliveryStatus property would have a ValueMember of "DeliveryStatus"; These days we would probably say `comboboxColumn.ValueMember = nameof(Person.Age);` or `comboboxColumn.ValueMember = nameof(Order.DeliveryStatus);` - the compiler will fill in the string for us, so that if we rename the property, the code updates automatically – Caius Jard Jan 23 '22 at 13:36

0 Answers0