如何将 DataGridViewComboBoxColumn 绑定到对象?
我正在尝试将 DataGridViewComboBoxColumn 绑定到 Foo 的实例,但是当我在网格上设置值时,我得到一个 ArgumentException 告诉我无法从 String 转换到福。
var data = (from item in someTable
select new { Foo = item.foo, Bar = item.Bar }).ToList();
grid.DataSource = data;
column.DataPropertyName = "Foo";
column.DataSource = (from foo in Foo select foo).ToList (); //foo is an instance of Foo
column.DisplayMember = "SomeNameField"; //Foo.SomeNameField contains a description of the instance
我错过了什么吗? 是否可以将数据绑定到复杂的对象?
更新:
我实现了一个 TypeConverter 并重写了 CanConvertFrom、CanConvertTo、ConvertTo、ConvertFrom。 现在我得到了
FormatException:DataGridViewComboBoxCell 值无效
什么想法吗?
I'm trying to bound a DataGridViewComboBoxColumn
to an instance of Foo, but when i set a value on the grid i got a ArgumentException
telling me that i can not convert from String to Foo.
var data = (from item in someTable
select new { Foo = item.foo, Bar = item.Bar }).ToList();
grid.DataSource = data;
column.DataPropertyName = "Foo";
column.DataSource = (from foo in Foo select foo).ToList (); //foo is an instance of Foo
column.DisplayMember = "SomeNameField"; //Foo.SomeNameField contains a description of the instance
Am i missing something? is it possible to databind to a complex object?
UPDATE:
I implemented a TypeConverter and overrided CanConvertFrom, CanConvertTo, ConvertTo, ConvertFrom. Now i'm getting
FormatException: The DataGridViewComboBoxCell value is not valid
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
DataGridViewComboBoxColumn 应始终具有组合框项列表中的所有可能值,否则将抛出“FormatException:DataGridViewComboBoxCell 值无效”。
如果您试图取回从一个组合框列中选择的值,您可以处理 DataGridView CellParsing 事件,并从 DataGridView.EditingControl 中获取所选项目,因为它将被设置为编辑列中的编辑控件。 这是一个示例:
您还可以通过处理单元格格式化事件来自定义对象在每个单元格上的显示方式,这里是显示任何对象或界面的 toString 的代码。
处理这两个事件应该足以在任何业务对象中显示和编辑数据,并且更容易编写类型转换器。 对于这项工作,请设置 DataGridView 和组合框列,如下所示:
不需要设置 DisplayMember 或 ValueMember 属性,只需确保组合框数据源列表具有 Foo 的所有可能值。
希望它有帮助。
the DataGridViewComboBoxColumn should always have all the possible values at the combobox Items list or it will throw "FormatException: The DataGridViewComboBoxCell value is not valid".
If you are trying to get back values chosen from one combobox column, you can handle the DataGridView CellParsing event, and get the selected item from DataGridView.EditingControl cause it will be set for editing control from the edited column. Here is a exemple:
You also can customize the way your objects are show on each cell by handling the cell Formatting Event, here is a code that display toString for any object or interface.
Handles this two events should be enough for show and edit data within any bussiness object and its easer then write type converters. For this work set you DataGridView and you combobox column as follow:
No DisplayMember or ValueMember Property need to be set, just make sure your combobox data source list has all the possible values for Foo.
Hope its helps.
你错过了一个可能的部分。
更新:
实际上,再次阅读您的问题后,我认为您面临着那个值得注意的错误。 遗憾的是,如果不使用自定义 TypeDescriptor/TypeConverter/BindingSource,则无法使其返回绑定对象。
用于绑定到复杂对象的答案。 默认情况下没有。 我为我当前的项目写了一篇相当不错的文章。 这涉及创建一个返回所有嵌套属性的自定义 TypeDescriptor/TypeConverter/BindingSource。 另一个“bug”,你不能使用“.” 对于成员分隔符,我不得不求助于“:”。
You are missing a possible piece.
UPDATE:
Actually, after reading your question again, I think you are facing that noted bug. There is unfortunately no way to make it return the bound object without using a custom TypeDescriptor/TypeConverter/BindingSource.
Answer for binding to a complex object. No by default. I wrote quite a nice one for my current project. This involves making a custom TypeDescriptor/TypeConverter/BindingSource that returns all the nested properties. Another 'bug', you cant use '.' for a member separator, I had to resort to ':' instead.
实际上,您可以在
DataGridViewComboBoxColumn
中使用复杂类型。例如:
Self
是:非常重要 - 需要重写复杂类型的“Equals”方法。
就我而言:
Actually, you can use a complex type in
DataGridViewComboBoxColumn
.For example:
Self
is:Very important - need to override 'Equals' method of a complex type.
In my case:
我不断遇到同样的问题,直到我发现如果不设置
ValueMember
也无法设置DataGridViewComboBoxCell
的DisplayMember
。同样,设置
ValueMember
而不是DisplayMember
也是失败的,您必须不定义或两者都定义。您的模型是 Foo,并且您当然希望 ComboBox 的值是项目本身。 为此,最简单的方法是在 foo 中创建一个属性,返回自身。
然后绑定变为:
这应该可以工作,并且 Cell 的 Value 应该是预期的 Foo 类型。
一个有趣的参考:DataGridViewComboBoxColumn 的问题
I was constantely hitting the same problem until I discovert that you cannot set the
DisplayMember
of theDataGridViewComboBoxCell
without setting theValueMember
too.In the same way, setting the
ValueMember
and not theDisplayMember
is a fail too, you have to define none or both.Your model is Foo, and you certainly want that the Value of the ComboBox be the item itself. For doing this, the simplest way is too create a Property in your foo, returning itself.
Then bindings becomes:
This should work, and the Value of the Cell should be of type Foo as expected.
An interesting reference: Problems with the DataGridViewComboBoxColumn
我也遇到了这个问题。 我想要一个 ComboBoxColumn 填充我的类中的 IssueNoteType,但是当我选择该值时,它试图将字符串传递给 DataBoundItem。 为了解决这个问题,我捕获了单元格更改事件,从所选名称中抓取了正确的对象,并将其应用于修改后的行的 DataBoundItem。
I was having an issue with this as well. I wanted a ComboBoxColumn filled with the IssueNoteType from my class but when I selected the value it was trying to pass a string to the DataBoundItem. To solve this problem I captured the cell change event grabbed the correct object from the selected name and applied it to the modified row's DataBoundItem.