在Vb.net中设置Datagirdviewcombobox列值

时间:2011-06-17 13:10:09

标签: datagridviewcombobox

我有两个datagridviewcombo列的datagrid,一列是动态填充,另一列是固定的硬编码值。 问题是我无法设置动态GridViewComboBox的值,当我尝试设置它时会生成连续错误。

  

System.FormateException:DataGridViewComboBoxCell Value无效。

加载网格的代码是

   Dim dt As DataTable
    dt = GetDataTable("Select valuecol,displayCol From mytable")  'GetDataTable gives me datatable
    cmbAntibiotics.DataSource = dt
    cmbAntibiotics.DisplayMember = "Antibiotics"
    cmbAntibiotics.ValueMember = "AntibioticsID"
    Dim Index As Integer

    Dim dgr As DataGridViewRow
    For Each dr As DataRow In dtFromDB.Rows 'This datatable is filled with database
        Index = dtFromDB.Rows.Count - 1
        GRDAntimicrobials.Rows.Add()
        GRDAntimicrobials.Rows(Index).Cells("cmbAntibiotics").Value = dr("AntibioticsID").ToString   'At this point it shows value (1,2,3) rather then showing its display members
        GRDAntimicrobials.Rows(Index).Cells("AntibioticsStatus").Value = dr("AntibioticsStatus").ToString
    Next

请帮帮我

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试将值分配给单元格上的任何内容,而不是实例化驻留在单元格中的对象,然后分配其值。我会尝试这样的事情:

Dim vComboBoxColumn As DropDownList = DirectCast(GRDAntimicrobials.Rows(index).Cells("cmbAntibiotics"))
vComboBoxColumn.Value = dr("AntibioticsStatus").ToString