使用 ComboBox 将 DataGridView 绑定到 DataTable 不起作用

Jay*_*yte 1 .net c# datatable datagridview datagridviewcombobox

我正在尝试创建一个绑定到 DataTable 的 DataGridView,其中一列是 ComboBox。代码运行,但在绑定后(不是在绑定数据时)出现以下错误:System.ArgumentException:DataGridViewComboBoxCell 值无效。

在 DataGridView 中,其中一列是 DataGridViewComboBoxColumn,它使用枚举(名为 StructureType)作为其源:

// ColumnStructure
// 
this.ColumnStructure.ValueType = typeof(structureType);
this.ColumnStructure.DataSource = Enum.GetValues(typeof(structureType));
this.ColumnStructure.HeaderText = "Structure";
this.ColumnStructure.Name = "ColumnStructure";
this.ColumnStructure.DataPropertyName = "Structure";
//
Run Code Online (Sandbox Code Playgroud)

当我在不使用 DataTable 的情况下填充 DataGridView 时,它工作得很好:

structureType? structure = GetStructure(part);
dgvObjectTypes.Rows.Add(name, type, structure, count);
Run Code Online (Sandbox Code Playgroud)

现在我想使用 DataTable 代替,但无法让它工作。数据表创建如下:

DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Type", typeof(string));
table.Columns.Add("Structure", typeof(DataGridViewComboBoxCell));
table.Columns.Add("Count", typeof(int));
Run Code Online (Sandbox Code Playgroud)

其他列工作得很好,但我无法让“结构”列工作。这是我尝试创建组合框的方法:

var cb = new DataGridViewComboBoxCell();
cb.ValueType = typeof(structureType);
cb.DataSource = Enum.GetValues(typeof(structureType));
cb.Value = (structureType)structure;
Run Code Online (Sandbox Code Playgroud)

之后,我只需为表创建行并将表设置为 DataGridView 的数据源:

table.Rows.Add(name, type, cb, count);
dgv.DataSource = table;
Run Code Online (Sandbox Code Playgroud)

我读过很多文章,其中指出在组合框中使用枚举会导致问题(例如:DataGridView linked to DataTable with Combobox columns based on enum),但这里似乎并非如此。我什至尝试使用显式类型的字符串数组,但仍然遇到相同的错误。我认为我对 DataGridViewComboBoxCell 做错了什么。

可能是什么问题呢?

Ňɏs*_*arp 5

您似乎缺少的步骤是为 CBO 提供名称和值。可以DataTable存储该值,DGV可以显示相关名称,但需要您帮助提供翻译。

private enum structureType
{ None, Circle, Square, Pyramid}
...

dtStruct = new DataTable();
dtStruct.Columns.Add("Name", typeof(string));
dtStruct.Columns.Add("Type", typeof(string));
dtStruct.Columns.Add("Structure", typeof(structureType));
dtStruct.Columns.Add("Count", typeof(int));

// autogen columns == true
dgv2.DataSource = dtStruct;

// create DataSource as list of Name-Value pairs from enum
var cboSrc = Enum.GetNames(typeof(structureType)).
                    Select( x => new {Name = x, 
                                      Value = (int)Enum.Parse(typeof(structureType),x)
                                      }
                           ).ToList();

// replace auto Text col with CBO col
DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();
cb.ValueType = typeof(structureType);
cb.DataSource = cboSrc;
cb.DisplayMember = "Name";          // important
cb.ValueMember = "Value";           // important
cb.HeaderText = "Structure";
cb.DataPropertyName = "Structure";  // where to store the value

dgv2.Columns.Remove(dgv2.Columns[2]);  // remove txt col
dgv2.Columns.Add(cb);
cb.DisplayIndex = 2;

// add data
dtStruct.Rows.Add("Ziggy", "Foo", structureType.Circle, 6);
Run Code Online (Sandbox Code Playgroud)

第一部分创建DataTable,请注意,结构列类型是structureType(或通常是int)。将DataTable存储数据,而不是DataGridViewComboBoxCell元素。如果数据来自数据库,则该列将int不是structureType已知类型。

DataSource然后根据枚举名称和值创建A。这为控件提供了显示名称并将值存储在DataTable.

如果 DGV 设置为自动生成列,您将需要将默认值替换TextBoxColumnComboBoxColumn. DataSource这是在设置之后、添加任何数据之前完成的。当数据来自数据库时(因此通常不存在空的类型表),您可以使用 ColumnAdded 事件将一列替换为另一列。

添加 CBO 列时重要的是设置ValueMemberDsiplayMember属性以提供值 <-> 名称转换,DataPropertyName以便它知道将所选值存储在DataTable.

在此输入图像描述