0

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?

Jake
  • 3,142
  • 4
  • 30
  • 48
  • `dictionary[fieldID]` gives the string value from the dictionary. You should set the value of the first cell to `fieldID` only. – Chetan Mar 11 '19 at 17:24
  • @ChetanRanpariya It is already set to `fieldID` only, I want it to be set to the string value from the dictionary. That's the goal. – Jake Mar 11 '19 at 17:25
  • Can you share to code which sets the value of cell to `dictionary[fieldID]`? Is it the first cell or second cell? – Chetan Mar 11 '19 at 17:29
  • I have updated the question to include the exact code I use, which is literally just a call to set `cell.Value`. The `cell` is the "first" cell, meaning that I want to change all of the FieldID's in the FieldID column from their ID numbers to their String equivalents. – Jake Mar 11 '19 at 18:01

1 Answers1

0

I managed to solve the problem on my own - although "solve" is a bit relative. I am still interested in the possibility of overwriting the actual column - but my solution is to just create a new column.

  1. Add a new, unbound column to the DataGridView, named "FieldName". For my example, column index 0 is the FieldID column, and column index 1 is the new FieldName column, which is currently empty.
  2. Modify the contents of column index 1 based on the contents of column index 0, via the dictionary lookup
  3. Hide column index 0 (doing this within the column properties GUI didn't work for me, so I did it in code)

Relevant code:

foreach(DataGridViewRow row in dataGridView.Rows) {
    for(int i = 0; i < row.Cells.Count; i++) {
        int fieldID = Convert.ToInt32(row.Cells[0].Value);
        String fieldName = dictionary[fieldID];
        row.Cells[1].Value = fieldName;
    }
}
FieldColumn.Visible = false;
Jake
  • 3,142
  • 4
  • 30
  • 48