Winforms绑定到datagridview中的组合框 [英] Winforms bind to a combobox in a datagridview

查看:224
本文介绍了Winforms绑定到datagridview中的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView,我绑定到一个BindingSource,显示一个对象的集合。我可以很容易地将TextboxColumn的数据绑定到DataGridView来显示来自数据源的数据。

I have a DataGridView which I have a BindingSource bound to, to display a collection of objects. I can easily databind TextboxColumn's to the DataGridView to display the data from the data source.

但是,数据源的一个属性是一个选择对象。我已经尝试使用网格数据绑定ComboboxColumn,但没有显示选择属性的文本的喜悦。

However, one of the properties of the data source is a choice object. I have tried to databind a ComboboxColumn with the grid but have had no joy in displaying the text of the choice property.

我有以下:

// 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中对象的属性。

But nothing seems to be displayed in the combobox. The "DisplayMember" and "ValueMember" are properties of the objects in "choiceCollection".

任何想法?

推荐答案

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

Ah, rookie mistake! I forgot the following line:

this.choiceDataGridViewComboBoxColumn.DataPropertyName = "ID";

这篇关于Winforms绑定到datagridview中的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆