如何填写DataGridViewComboBoxColumn?

时间:2018-05-04 14:51:43

标签: vb.net datagridview datagridviewcomboboxcell datagridviewcomboboxcolumn

很抱歉,如果这是显而易见的事情,但这是我使用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,我就不会再收到错误,但我也会得到空的组合框。

1 个答案:

答案 0 :(得分: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)