本文介绍了如何绑定一个DataGridViewComboBoxColumn到一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图约束的 DataGridViewComboBoxColumn 来富的一个实例,但是当我在网格设置值我有一个的ArgumentException 告诉我,我无法从字符串转换为富。

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?

更新:

我实施的TypeConverter和overrided CanConvertFrom,CanConvertTo,的ConvertTo,ConvertFrom。现在我就在

I implemented a TypeConverter and overrided CanConvertFrom, CanConvertTo, ConvertTo, ConvertFrom. Now i'm getting

FormatException:该的DataGridViewComboBoxCell值无效

任何想法?

推荐答案

您缺少一个可能的棋子。

You are missing a possible piece.

column.DataPropertyName = "Foo";
column.DisplayMember = "SomeNameField";
column.ValueMember = "Bar"; // must do this, empty string causes it to be
                            // of type string, basically the display value
                            // probably a bug in .NET
column.DataSource = from foo in Foo select foo;
grid.DataSource = data;



更​​新:

UPDATE:

其实,阅读后你的问题再次,我认为你正面临指出错误。不幸的是没有办法让它返回绑定的对象,而不使用自定义TypeDescriptor /的TypeConverter / BindingSource的。

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.

答案绑定到一个复杂的对象。默认情况下没有。我写了相当不错的一个我的当前项目。这涉及到制作定制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到一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 07:54