Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 在未绑定模式下以编程方式填充DataGridView组合框?_Vb.net_Winforms_Datagridview - Fatal编程技术网

Vb.net 在未绑定模式下以编程方式填充DataGridView组合框?

Vb.net 在未绑定模式下以编程方式填充DataGridView组合框?,vb.net,winforms,datagridview,Vb.net,Winforms,Datagridview,以下代码段(问题在这里:)用于在未绑定模式下填充datagridview中的组合框单元格: Dim dgvcc As DataGridViewComboBoxCell dgvcc = DataGridView1.Rows(2).Cells(0) dgvcc.Items.Add("comboitem1") dgvcc.Items.Add("comboitem2") 我也试着这样做,但我不得不注意到,强制转换操作是无效的,这正是VB给我的错误 我对代码进行了一些调整并尝试了一下,但仍然出现了相同的

以下代码段(问题在这里:)用于在未绑定模式下填充datagridview中的组合框单元格:

Dim dgvcc As DataGridViewComboBoxCell
dgvcc = DataGridView1.Rows(2).Cells(0)
dgvcc.Items.Add("comboitem1")
dgvcc.Items.Add("comboitem2")
我也试着这样做,但我不得不注意到,强制转换操作是无效的,这正是VB给我的错误

我对代码进行了一些调整并尝试了一下,但仍然出现了相同的铸造错误:

Dim dgvcc As Windows.Forms.DataGridViewComboBoxCell
dgvcc = Window.DataGridView1.Rows(2).Cells(0)
dgvcc.Items.Add("comboitem1")
dgvcc.Items.Add("comboitem2")
Window是DataGridView1对象所在表单的名称


任何人都可以告诉我一个简单的方法来填充一个datagridview在未绑定模式下的组合框。您也可以告诉我为什么它对我不起作用,为什么它对其他人起作用?

您使用的是GridViewComboxCell,而不是GridViewComboxColumn,并参考下面给出的代码片段,这样就可以了

    Dim cbState As DataGridViewComboBoxColumn
    cbState = DataGridView1.Columns("cbCol1")
    cbState.Items.Add("Karnataka")
    cbState.Items.Add("Andhra Pradesh")
上面的代码将为DataGridview提供如下结果

编辑:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim cbState As DataGridViewComboBoxColumn
    cbState = DataGridView1.Columns("cbCol1")
    cbState.Items.Insert(0, "Karnataka")
    cbState.Items.Add("Andhra Pradesh")

End Sub

Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    If e.ColumnIndex = 0 Then
        e.Value = "Karnataka"
    End If
End Sub

嘿,伙计,你知道如何设置组合框以在加载时将其中一个成员显示为默认成员吗?为项目添加索引,并将默认的一个以0为索引。很抱歉,我不知道如何做。你可以使用DataGridView的Bubbled事件,请参考代码一次