Note that there are some explanatory texts on larger screens.

plurals
  1. PONon-String values in DataGridViewComboBoxColumn causing DataErrors in DataGridView
    primarykey
    data
    text
    <p>I have a programmtically filled DataGridView with some <code>DataGridViewComboBoxColumn</code> entries. These ComboBoxes hold custom types that have their <code>ToString()</code> overwritten to hide their internal guts from the user.</p> <p>The Problem: whenever I change the value of the ComboBox and select another cell, the <code>CellValidating</code> is called, the FormattedValue is the string-result of my custom FooBar.ToString() (should be ok). In <code>CellEndEdit</code>, the ValueType is suddenly a string and not my custom type anymore and I get error either watching row.Cell-Member in VS or doing a mouse-over on the changed cell (translated from german):</p> <p>"DataGridView-Exception: <code>System.ArgumentException</code>: The <code>DataGridViewComboBoxCell</code>-Value is invalid. Handle the DataError-Exception to replace this dialog"</p> <p>This is how the column is constructed - colFoo is a <code>DataGridViewComboBoxColumn</code>:</p> <pre><code> 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); </code></pre> <p>The Grid gets filled like this:</p> <pre><code> foreach (FooLine line in SomeFooLineArray) { DataGridViewRow newRow = ((DataGridViewRow)dataGridInstance.RowTemplate.Clone()); newRow.Cells[1].Value = line.Foo; // FooBar instance field dataGridInstance.Rows.Add(newRow); } </code></pre> <p>Any experiments in <code>CellValidating</code> or <code>CellFormatting</code> failed, I always get this message. </p> <p>How can I add custom types into the existing <code>DataGridView</code> without overwriting too much? Almost every other Component (including dropboxes) supports custom types just fine, only the step of copying the value from the Combo to the grid seams to be the problem.</p> <p>As asked, the FooBar-implementation looks like this:</p> <pre><code> 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; } } </code></pre> <p>It does a little bit more, but that is the meaningful functionality encapsulated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload