【问题标题】:Datagridview combo box: the combobox cell shows the ValueMember instead of DisplayMember and "DataGridViewComboBoxCell value is not valid"? [closed]Datagridview 组合框:组合框单元格显示 ValueMember 而不是 DisplayMember 和“DataGridViewComboBoxCell 值无效”? [关闭]
【发布时间】:2014-06-25 22:17:51
【问题描述】:

我有以下代码在数据网格视图中设置组合框。

class Lookup { byte Id {get; set;} string Text {get; set;} }
List<Lookup> rs = GetData(...);
cb.DataSource = rs;
cb.DisplayMember = "Text";
cb.ValueMember = "Id";
cb.DataPropertyName = "LookupId"; // The column of data source of grid view is also byte

但是,我不断收到“DataGridViewComboBoxCell 值无效”的错误消息。什么可能导致问题?选择值后,组合框单元格显示数字(Id)而不是“Text”?

更新:

这个问题是由LookUp中定义的错误类型引起的。

【问题讨论】:

  • 我已删除问题中的元请求以关闭它,并投了赞成票(错字或不可复制)。

标签: c# winforms


【解决方案1】:

在“查找”类中,重写 ToString() 函数并返回 Text 属性的值。由于您使用的是对象,因此不需要显式分配 DisplayMember。

【讨论】:

    【解决方案2】:

    尝试使用 BindingList 看看这个例子:

    BindingList with my class populating a ComboBox using a property of it?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-27
      • 2013-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多