“将”DataGridViewColumn“转换”为DataGridViewComboBoxColumn VB.NET

时间:2015-11-27 15:09:36

标签: vb.net datagridview

我目前基于for i in range(len(data)): byte = data[i:i+1] print(byte) 中的静态表格有一个DataGridView。我想将VB.NET中的一列“转换”为dgv。我的想法是简单地生成列,将其插入表中,然后将感兴趣的列中的值复制到新的DataGridViewComboBoxColumn,如下所示:

ComboBoxColumn

但是,新列中的单元格仍为空;所以我认为他们没有复制。但是通过取消注释 Dim table = DbModel.GetEntries() If Not table Is Nothing Then With DataGridView1 .DataSource = table .RowHeadersVisible = False .AllowUserToAddRows = True .SelectionMode = DataGridViewSelectionMode.CellSelect With .Columns("ID") .Width = 31 .ReadOnly = True End With Dim comboBoxColumn As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn() comboBoxColumn.ValueType = GetType(String) Dim myList As New List(Of String) myList.Add("Choice 1") comboBoxColumn.DataSource = myList DataGridView1.Columns.Insert(2, comboBoxColumn) For i = 1 To DataGridView1.RowCount - 1 If Not DataGridView1.Rows(i).Cells(3).Value Is Nothing Then DataGridView1.Rows(i).Cells(2).Value = DataGridView1.Rows(i).Cells(3).Value.ToString End If 'MsgBox(comboBoxColumn.DataGridView.Rows(i).Cells(2).Value) Next End With End If - 循环中的注释,我清楚地看到它们被复制了。有人可以告诉我哪里出错了?谢谢,Tingis

2 个答案:

答案 0 :(得分:1)

我不认为您只想使用名为DataGridViewComboBoxColumn的项Choice 1,所以您的问题可能是您尝试设置的值不是有效选择。< / p>

因此,您必须使用原始列中包含的每个可能值填充DataGridViewComboBoxColumn

Dim comboBoxColumn As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn()
comboBoxColumn.ValueType = GetType(String)

DataGridView1.Columns.Insert(2, comboBoxColumn)

For i = 0 To DataGridView1.RowCount - 1
    If Not IsNothing(DataGridView1.Rows(i).Cells(3).Value) Then
        comboBoxColumn.Items.Add(DataGridView1.Rows(i).Cells(3).Value.ToString)
    End If
Next i

For i = 0 To DataGridView1.RowCount - 1
    If Not DataGridView1.Rows(i).Cells(3).Value Is Nothing Then
        DataGridView1.Rows(i).Cells(2).Value = DataGridView1.Rows(i).Cells(3).Value.ToString
    End If
Next i

请注意,行索引从0开始。

答案 1 :(得分:0)

如果你只是添加

,tezzo的解决方案就有效
comboBoxColumn.DataPropertyName = "<Your column name goes here>"

创建列时。而且你甚至不必遍历行并手动设置值。