【问题标题】:DataGridViewComboBoxColumn - type of items in the drop down listDataGridViewComboBoxColumn - 下拉列表中的项目类型
【发布时间】: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


【解决方案1】:

解决方案是在 Field 类中创建属性 Self

public Field Self
        {
            get { return this; }
        }

并设置为组合框列的 ValueMember。
我以为没有指定 ValueMember this 是默认返回值,我错了。

【讨论】:

  • 感谢您的提示。 WPF 绑定更好。
猜你喜欢
  • 2012-05-15
  • 2016-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
  • 1970-01-01
  • 2023-01-12
相关资源
最近更新 更多