I'm working on an application that displays a DataGridView of a table, which looks like this:
FieldID | Value
---------------
1 | Jane
2 | Doe
I also have a Dictionary<int, String>
which lets me run lookups from the FieldID to a String version of the Field name (First Name, Last Name, etc.)
Following the advice here, How to create LookUp fields in DataGridView?, I'm trying to set the cells in the FieldID column to their dictionary[fieldID]
lookup. However, when I set the cell.Value
, I get the following error (runtime error, popping up in my WinForms application and not Visual Studio):
The following exception occurred in the DataGridView:
System.Exception: First Name is not a valid value for Int32. ----->
System.FormatException: Input string was not in a correct format
at System.number.StringToNumber...
etc.
The code to set the cell is as follows:
String value = dictionary[cell.Value]; //value = "First Name"
cell.Value = value; //Error occurs on this line
My problem is, I don't know why/where the DataGridView would be converting to an Int32 - I never explicitly stated that the column containing FieldID's was strictly int's. If it did interpret it that way, I've tried running the following prior to assigning the String value to the cell:
dataGridView.Columns[0].CellTemplate.ValueType = typeof(String);
and
cell.ValueType = typeof(String);
Neither seem to make any difference, though. How can I stop the implicit conversion to int?