Set Value of DataGridViewComboBoxCell

N

news.microsoft.com

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.

??
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top