Hello-
I'm having a dickens of a time trying to set the value on a
DataGridViewComboBoxColumn. I actually have two DataGridViewComboBoxColumns
in my datagrid view.
First, I'm setting up the grid/column as follows..
DataGridViewComboBoxColumn someColumn = new
DataGridViewComboBoxColumn();
panelColumn.DisplayIndex = 0;
panelColumn.HeaderText = "SomeHeader";
panelColumn.DataSource = myDataSource; // myDataSource is of
type List<MyInterestingClass>
panelColumn.DisplayMember = "StringMember"; // StringMember
is of type string
panelColumn.Name = "Whatever";
panelColumn.ValueMember = "InterestingValue"; //
InterestingValue is a type of a custom class, i.e., MyInterestingClass
frm.dataGridView1.Columns.Add(panelColumn);
However, when I try to set the cell value, I get an exception:
DataGridViewComboBoxCell cell =
(DataGridViewComboBoxCell)frm.dataGridView1["Whatever", row];
cell.Value = cell.Items[0]; // exception: Unable to
cast object of type 'MyInterestingClass' to type 'System.String'.
call.Value = myDataSource[0]; // exception: Unable to
cast object of type 'MyInterestingClass' to type 'System.String'.
I don't understand why it is trying to cast to System.String... perhaps to
display the value in the DataGridViewComboBoxCell? This doesn't make sense,
though, because you set the .DisplayMember.
??