简体   繁体   English

C#Winforms DatagridviewCombobox异常字符串无法转换为类

[英]C# Winforms DatagridviewCombobox exception String cannot be converted to class

I am encountering an exception when selecting a new value from a datagridviewcombobox(dropdown menu) control embedded in a datagridview. 从嵌入在datagridview中的datagridviewcombobox(下拉菜单)控件中选择新值时遇到异常。 The combobox is populated by a BindingSource, which is populated with instances of my class. 组合框由BindingSource填充,该源由我的类的实例填充。 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. 我可以正确显示菜单中的选项,然后选择一个选项,但是将焦点更改为新控件(提交我猜为所做的更改)会导致出现异常:从System.String到myclass的无效转换。 The stack trace (if Im using that word right)shows the source was 堆栈跟踪(如果Im使用正确的单词)显示源为

System.Windows.Forms.DataGridView.PushFormattedValue cascading down to System.Convert.DefaultToType System.Windows.Forms.DataGridView.PushFormattedValue级联到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). 我有一个名为Occupant的空类,没有任何属性(当Occupant也具有字符串Name属性时,就会出现问题,并非如此)。 I have a BindingSource called OccupantSource, with its DataSource pointing to Occupant. 我有一个名为OccupantSource的BindingSource,其DataSource指向Occupant。

I also have a class called Car, with one Occupant property called Driver. 我也有一个名为Car的类,具有一个名为Driver的Occupant属性。

In my Form_Load(), I call OccupantSource.AddNew() twice, and call CarSource.AddNew() once. 在我的Form_Load()中,我两次调用OccupantSource.AddNew(),然后一次调用CarSource.AddNew()。

I have a DataGridView control, whose DataSource is CarSource (the BindingSource consisting of Cars). 我有一个DataGridView控件,其数据源是CarSource(由Car组成的BindingSource)。 The DGV has one column, displaying the Driver property of the cars in CarSource. DGV具有一列,显示CarSource中汽车的Driver属性。 It is a DataGridViewComboBoxColumn, with DataPropertyName set to driver. 它是一个DataGridViewComboBoxColumn,其中DataPropertyName设置为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. 所以我想要在Datagridview中显示汽车行,其中一列是组合框,我可以下拉菜单并从Occupant的现有实例中选择一个驱动程序。 But I get the exception. 但我得到了例外。

Is this something I'm misunderstanding? 这是我误会的东西吗? Can you not use instances of a class to populate a DataGridViewComboBox? 您不能使用类的实例来填充DataGridViewComboBox吗?

I ran into exactly the same problem and was scratching my head, using my google-fu for hours attempting to solve it. 我遇到了完全相同的问题,并用我的google-fu试图解决它的时间挠头了。 This link helped me finally gave me a good explanation. 这个链接帮助我终于给了我一个很好的解释。 http://www.pcreview.co.uk/forums/datagridview-combobox-column-error-listing-objects-t2344961.html 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. 我修复它的方法是更换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. 我在返回“ this”的类上引用了“ Self”-我在DisplayMember和ValueMember中都使用了此对象,以为它只是ToString()来自DisplayMember的属性。

Reading your explanation, you might not have DisplayMember and ValueMember set at all? 阅读您的解释,您可能根本没有设置DisplayMember和ValueMember吗? 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. 如果是这种情况,请尝试正确设置它们(并且不要对显示成员使用对“ this”的引用!),这可能会解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM