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?
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 bothValueMember
andDisplayMember
(controls how the renders in the cell) so setting the Value tointerface.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.I am having the same problem. After populating my ComboBox column in the (unbouod) DataGrid, I solved my problem by setting the ValueMember property of the
DataGridViewComboBoxColumn
Apparently, just relying on theToString()
property of the objects in the ComboBox is not enough.Actual code:
Here's my simple solution when using enums
you can do that just after "InitializeComponent();"