0

我有一个DataGridView我想以编程方式填写的内容。通过创建 aDataTable并将其通过 a 绑定BindingSource到 a DataGridView, 中的列将DataGridView创建为TextBoxColumns。下面的代码示例:

Dim dataTable As DataTable = New DataTable()
dataTable.Columns.AddRange(New DataColumn() {New DataColumn("TextBoxColumn1"),
                                             New DataColumn("TextBoxColumn2"),
                                             New DataColumn("ComboBoxColumn"),
                                             New DataColumn("TextBoxColumn3"),
                                             New DataColumn("TextBoxColumn4")})

Dim bindingSource As BindingSource = New BindingSource
bindingSource.DataSource = dataTable
dataGridView.DataSource = bindingSource

使“ComboBoxColumn”列在ComboBoxColumn中的最佳方法是DataGridView什么?

我可以手动创建 a ComboBoxColumnDataGridView然后将其绑定到 a DataColumn,但我想知道是否有更好的方法,比如在中设置属性DataColumn或类似的东西。

4

1 回答 1

2

遵循@Codesleuth 的建议,我将自动生成的替换为DataGridViewTextBoxColumn这样的DataGridComboBoxColumn

Dim comboBoxColumn As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn()
comboBoxColumn.DataPropertyName = "ComboBoxColumn"
comboBoxColumn.DataSource = comboBoxBindingSource
comboBoxColumn.DisplayMember = "ComboBoxColumn"
comboBoxColumn.ValueMember = "ComboBoxColumn"
DataGridView.Columns.RemoveAt(COLUMN_INDEX)
DataGridView.Columns.Insert(COLUMN_INDEX, comboBoxColumn)
于 2013-06-28T14:34:10.690 回答