I have a programmatically populated DataGridView with some DataGridViewComboBoxColumn elements. These ComboBoxes contain custom types that have their ToString() overwritten to hide their guts from the user.
Problem: whenever I change the value of the ComboBox and select another cell, CellValidating is CellValidating , FormattedValue is the string result of my custom FooBar.ToString () (should be fine). In CellEndEdit , the ValueType value is a string, not my custom type, and I get an error either when viewing row.Cell-Member in VS, or when using the mouse in a modified cell (translated from German):
"DataGridView-Exception: System.ArgumentException : DataGridViewComboBoxCell -Value is not valid. Refer to DataError-Exception to replace this dialog box
This creates a column - colFoo - this is DataGridViewComboBoxColumn :
dataGridInstance.Columns[1].CellTemplate.Value = new FooBar("bla", 31); dataGridInstance.Columns[1].CellTemplate.ValueType = typeof(FooBar); foreach (FooBar foo in FooBarArray) colSymbol.Items.Add(sym);
The grid is filled as follows:
foreach (FooLine line in SomeFooLineArray) { DataGridViewRow newRow = ((DataGridViewRow)dataGridInstance.RowTemplate.Clone()); newRow.Cells[1].Value = line.Foo;
Any experiments in CellValidating or CellFormatting failed, I always get this message.
How to add custom types to an existing DataGridView without overwriting too much? Almost every other component (including dropboxes) supports custom types only in order, only the problem of copying values ββfrom Combo to mesh seams is a problem.
As specified, the FooBar implementation is as follows:
internal class FooBar { private readonly int id; private readonly string name; internal FooBar(string n, int i) { id = i; name = n; } public override string ToString() { return name; } }
This is a bit more, but it is significant functionality encapsulated.