Sorry if this is something that should be obvious, but this is my first project using VB.NET, and some things are still beyond me.
I'm trying to setup a combobox inside a DataGridView but I keep getting
System.ArgumentException: DatagridviewComboBoxCell value not valid
I've been googling this for 2 hours now and to me it seems I'm setting up thing correctly, but probably something is amiss.
Dim imageCol As DataGridViewImageColumn
Dim checkCol As DataGridViewCheckBoxColumn
Dim col As DataGridViewColumn
Dim comboCol As DataGridViewComboBoxColumn
Dim ds As DataSet
Dim som As New SomStructure
Dim somministrazioni() As SomStructure = {}
With dgvListaAttivita
.Columns.Clear()
.AutoGenerateColumns = False
.ReadOnly = False
.EditMode = DataGridViewEditMode.EditOnEnter
.CausesValidation = False
somministrazioni.Clear
ds = getSomministrazioni(codevalue, Today())
If ds IsNot Nothing Then
For Each row As DataRow In ds.Tables(0).Rows
som.idOspite = row(0)
som.nomeOspite = row(1)
som.descrizioneSomministrazione = row(2)
som.idOperatore = row(3)
som.nomeOperatore = row(4)
som.preparata = False
somministrazioni.Add(som)
Next
End If
.DataSource = somministrazioni
imageCol = New DataGridViewImageColumn
imageCol.Width = 25
imageCol.ImageLayout = DataGridViewImageCellLayout.Normal
imageCol.Description = "delete"
imageCol.Image = My.Resources.note
.Columns.Add(imageCol)
col = New DataGridViewColumn
col.DataPropertyName = "descrizioneSomministrazione"
col.HeaderText = "Somministrazione"
col.ValueType = GetType(String)
col.CellTemplate = New DataGridViewTextBoxCell
col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
col.ReadOnly = True
.Columns.Add(col)
comboCol = New DataGridViewComboBoxColumn
Dim dt As DataTable = dsOperatori.Tables(0)
comboCol.DataSource = dt
comboCol.DisplayMember = "display"
comboCol.ValueMember = "idoperatore"
comboCol.DataPropertyName = "idOperatore"
comboCol.HeaderText = "Operatore"
comboCol.ValueType = GetType(Integer)
comboCol.CellTemplate = New DataGridViewComboBoxCell
comboCol.Width = 150
.Columns.Add(comboCol)
checkCol = New DataGridViewCheckBoxColumn
checkCol.DataPropertyName = "preparata"
checkCol.HeaderText = "P."
checkCol.ValueType = GetType(Boolean)
checkCol.CellTemplate = New DataGridViewCheckBoxCell
checkCol.Width = 20
.Columns.Add(checkCol)
End With
As I understand it it should be like this:
comboCol.DataSource = dt
comboCol.DisplayMember = "display"
comboCol.ValueMember = "idoperatore"
specifies the datasource and key/display values of the comboboxes in the column
comboCol.DataPropertyName = "idOperatore"
Is the name of the column linked to both the DataSource of the DataGridView and the ValueMember of the ComboBox, and that should display different selections on different rows.
If I remove the DataPropertyName from the code I don't get the error anymore, but I also get empty comboboxes.
Try commenting out the template line:
comboCol = New DataGridViewComboBoxColumn
Dim dt As DataTable = dsOperatori.Tables(0)
comboCol.DataSource = dt
comboCol.DisplayMember = "display"
comboCol.ValueMember = "idoperatore"
comboCol.DataPropertyName = "idOperatore"
comboCol.HeaderText = "Operatore"
comboCol.ValueType = GetType(Integer)
'comboCol.CellTemplate = New DataGridViewComboBoxCell
comboCol.Width = 150
.Columns.Add(comboCol)