【发布时间】:2009-02-23 14:08:59
【问题描述】:
我有一个包含 ComboBox 列的 DataGridView。我用 Field 类型的项目填充此列的列表:
DataGridViewComboBoxColumn fieldsColumn = argumentsDataGridView.Columns["field"] as DataGridViewComboBoxColumn;
foreach (Field field in SessionData.Fields)
fieldsColumn.Items.Add(field);
fieldsColumn.DisplayMember = "Name";
然后在某些用户的操作之后,我在此列的单元格中输入了一个值,如下所示:
private void AddArgument(string argumentName, Field field)
{
int index = argumentsDataGridView.Rows.Count;
argumentsDataGridView.Rows.Add(new DataGridViewRow());
DataGridViewRow newRow = argumentsDataGridView.Rows[index];
newRow.Cells["nameArg"].Value = argumentName;
-> newRow.Cells["field"].Value = field;
}
如果我现在访问单元格的值,它属于 Field 类型。如果我从组合中选择不同的项目,则单元格的值将变为字符串。我该如何处理?我需要 Field 类型的项目。
【问题讨论】:
-
你是怎么让它工作的?当我运行一个简单的测试时,我得到一个 System.ArgumentException?
-
你从哪里得到这个异常?
-
我创建了一个具有三个属性的虚拟类。然后,我创建了其中一些对象的列表,并将它们添加到 ComboBoxColumn 中,就像在您的第一个方法中一样。然后,我复制了您的第二种方法,添加了另一行并添加了另一个对象,但随后得到了一个异常。
-
你在处理 DataError 事件吗?
标签: c# .net datagridview