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?