【问题标题】:Winforms bind to a combobox in a datagridviewWinforms 绑定到 datagridview 中的组合框
【发布时间】:2013-08-10 10:02:13
【问题描述】:

我有一个绑定了 BindingSource 的 DataGridView,用于显示对象集合。我可以轻松地将 TextboxColumn 数据绑定到 DataGridView 以显示来自数据源的数据。

但是,数据源的属性之一是选择对象。我尝试将 ComboboxColumn 与网格进行数据绑定,但在显示选择属性的文本时没有任何乐趣。

我有以下几点:

// bind to the datagrid
this.datagridBindingSource.DataSource = collectionForDatagrid;
this.dataGrid.DataSource = this.datagridBindingSource.DataSource;

// now bind the collection of choices to the combobox column
this.choiceDataGridViewComboBoxColumn.DataSource = choiceCollection;

// set the display and value members of the combobox
this.choiceDataGridViewComboBoxColumn.DisplayMember = "Name";
this.choiceDataGridViewComboBoxColumn.ValueMember = "ID";

但组合框中似乎没有显示任何内容。 “DisplayMember”和“ValueMember”是“choiceCollection”中对象的属性。

有什么想法吗?

【问题讨论】:

  • 你想绑定到choiceDataGridViewComboBoxColumn的数据源中的Property是什么?
  • 您和我同时注意到了这个问题!...我实际上并没有设置“DataPropertyName”属性。按下“post”的那一秒,我想到答案的次数!

标签: c# winforms data-binding datagridview combobox


【解决方案1】:

啊,菜鸟的错误!我忘记了以下行:

this.choiceDataGridViewComboBoxColumn.DataPropertyName = "ID";

【讨论】:

    猜你喜欢
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 2012-08-10
    • 1970-01-01
    • 2011-09-15
    • 2010-09-12
    • 1970-01-01
    相关资源
    最近更新 更多