I have a DataGridView that has a ComboBox column. I populate this column's list with items of a type Field :
DataGridViewComboBoxColumn fieldsColumn = argumentsDataGridView.Columns["field"] as DataGridViewComboBoxColumn;
foreach (Field field in SessionData.Fields)
fieldsColumn.Items.Add(field);
fieldsColumn.DisplayMember = "Name";
Then after some user's action I put a value in the cell of this column like this:
private void AddArgument(string argumentName, Field field)
{
int index = argumentsDataGridView.Rows.Count;
argumentsDataGridView.Rows.Add(new DataGridViewRow());
DataGridViewRow newRow = argumentsDataGridView.Rows[index];
newRow.Cells["nameArg"].Value = argumentName;
-> newRow.Cells["field"].Value = field;
}
If I now access the cell's Value, it is of a type Field . If I select different item from the combo, the cell's Value becomes a string. How can I handle it? I need items of type Field .
the solution was to create a property Self in class Field :
public Field Self
{
get { return this; }
}
and set is as a ValueMember of the combo box column.
I thought that without specifing ValueMember this is a default return value and I was wrong.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.