5
votes

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?

1
May be the selected item/current item displayed from the combobox is coming as object but internally it could be a string. And out of focus is internally calling commit event which is trying to convert.Zenwalker
What is the Exception.Message that you recive?MoonKnight
I forgot to mention, there is a BindingSource called CarSource, and the DGV's ComboBoxBolumn's datasource is set to OccupantSource. There are no strings in the project, and the exception message is here: img508.imageshack.us/img508/2586/unledbfv.jpgMoz
I got this Error , like you.any suggestion ?Esi

1 Answers

6
votes

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.