I couldn't find a question on SO that exactly matched my problem.
Similar to this question and this question, I'm setting the DataSource
on a DataGridViewComboBoxColumn
to a list of things. In my case the things are simple types like doubles and ints, so the answers talking about ValueMembers and DisplayMembers don't do me a lot of good. When the user selects a value I get the dreaded "DataGridViewComboBoxCell value is not valid" error.
I could swallow the error with an empty dataGridView_DataError handler, but that is obviously a bad way to go.
I found the answer here. It's also mentioned in this answer to the second link in my question. When setting the DataSource
to a list of anything that's not a string, set the ValueType
of the column to typeof(<your data type>)
IList<double> kvChoices;
// Populate kvChoices...
DataGridViewComboBoxColumn kvCol =
dataGridView1.Columns[0] as DataGridViewComboBoxColumn;
kvCol.DataSource = kvChoices;
kvCol.ValueType = typeof(double);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With