Consider a data entry form where the user needs to enter times in military format. They wish to key from a keypad to speed up their work but don’t want to key the colon (:) between the hours/minutes.
The column is formatted to allow time and will automatically convert the time to the standard AM/PM formats. Unfortunately, it requires the colon. Therefore a conversion method should be run on this input to attempt to convert it to a time value.
The method to do this is not part of the answer I’m looking for. It could represent any reason why someone would want to change a value keyed by a user into some other value. The key is that it needs to happen before validation so the DataError event is not raised.
I assumed that CellValidating would be the right place for this so I wrote what I expected to work:
private void sampleDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
// Ensure we're looking at the column that requires our attention
if (sampleDataGridView.Columns[e.ColumnIndex].Name == "timeCol")
sampleDataGridView[e.ColumnIndex, e.RowIndex].Value = convertMilitary(e.FormattedValue);
}
When stepping into this code after the cell is written to, the event args tell me that the formatted value is indeed what was keyed, but attempting to set this to the current value of the cell doesn’t help validation. On closer inspection I learned that the value is still what it was prior to user entry.
This leaves me at an impasse because I don’t see where else I would put this. I feel that I’m in the right spot but I’m missing something. Where do I put this conversion so the DataError event isn’t raised from a failed validation?
Its possible that there is a different property I need to set? Its like I should be changing what the user keyed, not the cell value directly like I am attempting to do…
Have you looked at using the CellParsing event? I seem to recall using this when I needed to do something similar.