2

对不起,如果这应该是显而易见的,但这是我使用 VB.NET 的第一个项目,有些事情仍然超出我的能力范围。

我正在尝试在 DataGridView 中设置一个组合框,但我不断收到

System.ArgumentException:DatagridviewComboBoxCell 值无效

我已经在谷歌上搜索了 2 个小时,对我来说,我似乎设置正确,但可能有些不对劲。

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

据我了解,它应该是这样的:

comboCol.DataSource = dt
comboCol.DisplayMember = "display"
comboCol.ValueMember = "idoperatore"

指定列中组合框的数据源和键/显示值

comboCol.DataPropertyName = "idOperatore"

是链接到 DataGridView 的 DataSource 和 ComboBox 的 ValueMember 的列的名称,并且应该在不同的行上显示不同的选择。

如果我从代码中删除 DataPropertyName,我不会再收到错误,但我也会得到空的组合框。

4

1 回答 1

0

尝试注释掉模板行:

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)
于 2018-05-07T14:17:33.873 回答