Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: "DataGridViewComboBoxCell value is not valid." DataSource is list of basic type

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.

like image 731
Chuck Wilbur Avatar asked Nov 19 '12 20:11

Chuck Wilbur


1 Answers

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);
like image 116
Chuck Wilbur Avatar answered Nov 18 '22 04:11

Chuck Wilbur