C# Winforms DatagridviewCombobox 异常字符串无法转换为类
从嵌入 datagridview 的 datagridviewcombobox(下拉菜单)控件中选择新值时遇到异常。组合框由 BindingSource 填充,其中填充了我的类的实例。我可以正确显示菜单中的选项,并选择一个,但是将焦点更改为新控件(我猜是提交更改)会导致出现异常:从 System.String 到 myclass 的转换无效。堆栈跟踪(如果我正确使用该词)显示源是
System.Windows.Forms.DataGridView.PushFormattedValue 级联至 System.Convert.DefaultToType
下面是更明确的解释(抱歉它太长了,但我想让它可重现):
我有一个名为 Occupant 的空类,没有属性(当 Occupant 也有一个字符串 Name 属性时存在问题,所以不是那样的)。我有一个名为 OccupantSource 的 BindingSource,其 DataSource 指向 Occupant。
我还有一个名为 Car 的类,其中有一个名为 Driver 的 Occupant 属性。
在 Form_Load() 中,我调用 OccupantSource.AddNew() 两次,并调用 CarSource.AddNew() 一次。
我有一个DataGridView控件,其DataSource是CarSource(由Cars组成的BindingSource)。 DGV 有一列,显示 CarSource 中汽车的 Driver 属性。它是一个 DataGridViewComboBoxColumn,DataPropertyName 设置为驱动程序。
所以我想要的是在 Datagridview 中显示一排汽车,其中一列是一个组合框,我可以下拉菜单并从现有的 Occupant 实例中选择驾驶员。但我得到了例外。
这是我的误解吗?不能使用类的实例来填充 DataGridViewComboBox 吗?
I am encountering an exception when selecting a new value from a datagridviewcombobox(dropdown menu) control embedded in a datagridview. The combobox is populated by a BindingSource, which is populated with instances of my class. I can display the options in the menu properly, and select one, but changing focus to a new control (committing the change I guess) causes an exception to appear: Invalid Cast from System.String to myclass. The stack trace (if Im using that word right)shows the source was
System.Windows.Forms.DataGridView.PushFormattedValue
cascading down to
System.Convert.DefaultToType
A more explicit explanation is below (sorry its so long, but I wanted to make it reproducable):
I have an empty class called Occupant, with no properties(the problem exists when Occupant also has a string Name property so it's not that). I have a BindingSource called OccupantSource, with its DataSource pointing to Occupant.
I also have a class called Car, with one Occupant property called Driver.
In my Form_Load(), I call OccupantSource.AddNew() twice, and call CarSource.AddNew() once.
I have a DataGridView control, whose DataSource is CarSource (the BindingSource consisting of Cars). The DGV has one column, displaying the Driver property of the cars in CarSource. It is a DataGridViewComboBoxColumn, with DataPropertyName set to driver.
So what I want is to show rows of cars in the Datagridview, with one of the columns being a combobox I can dropdown and choose a driver from existing instances of Occupant. But I get the exception.
Is this something I'm misunderstanding? Can you not use instances of a class to populate a DataGridViewComboBox?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了完全相同的问题,并且绞尽脑汁,使用我的 google-fu 几个小时试图解决它。这个链接帮助我终于给了我一个很好的解释。
http://www.pcreview.co .uk/forums/datagridview-combobox-column-error-listing-objects-t2344961.html
我修复它的方法是更改显示成员。
我在返回“this”的类上引用了“Self” - 我将其用于 DisplayMember 和 ValueMember,认为它只是 ToString() 来自 DisplayMember 的属性。
阅读您的解释,您可能根本没有设置 DisplayMember 和 ValueMember ?如果是这种情况,请尝试正确设置它们(并且不要使用对“this”的引用作为显示成员!),它可能会修复它。
I ran into exactly the same problem and was scratching my head, using my google-fu for hours attempting to solve it. This link helped me finally gave me a good explanation.
http://www.pcreview.co.uk/forums/datagridview-combobox-column-error-listing-objects-t2344961.html
The way I fixed it was to change up the DisplayMember.
I had a refernce to 'Self' on the class that returned 'this' - I was using this for both DisplayMember and ValueMember thinking that it would just ToString() the property from DisplayMember.
Reading your explanation, you might not have DisplayMember and ValueMember set at all? If this is the case, try setting them correctly (and don't use a reference to 'this' for display member!) and it might fix it.