Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridViewComboBoxCell Binding - "value is not valid"

I'm trying to bind separate ComboBox cells within a DataGridView to a custom class, and keep getting an error

DataGridViewComboBoxCell value is not valid

I'm currently assigning the data source for the cell to an IList<ICustomInterface> from a Dictionary I've got. Upon setting the data source however, the index for the ComboBoxCell isn't set, so it has an invalid value selected.

I'm trying to figure out how to get it to select a real value, e.g. the 0th item within the list it has been given to remove this error, or find another way to solve the problem. Anyone have any suggestions?

like image 962
Ian Avatar asked Mar 17 '09 15:03

Ian


1 Answers

I managed to find the solution not long after posting the question. For anyone else:

The problem was that I was trying to assign the DataGridViewComboBoxCell.Value to an object, expecting that because the Cell was bound to a data source that it would automatically find the object in the source and update.

This wasn't actually the case, you actually need to set the value equal to that of the ValueMember property for it to correctly update the value and binding. I believe I was using a property 'Name' for both ValueMember and DisplayMember (controls how the renders in the cell) so setting the Value to interface.ToString() (rather than the interface instance) works for the majority of cases. Then I catch and ignore any DataError exceptions that occur while I'm changing the source around.

like image 90
Ian Avatar answered Sep 21 '22 00:09

Ian