Situation:
I'm writing a Winforms app using C# in VS2013 with .NET4.0.
To carry out cell level validation in a DataGridView I handle the CellValidating event. When validating I access the user input values with
dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value
For the validation of existing rows this works fine.
Issue:
When the user types something into a cell on a new row and then presses, for example, tab to move to the next cell the CellValidating event fires but Value always contains null.
Question:
How do I access what the user has typed in these circumstances? I'm thinking that I should maybe EndEdit before validating but I thought CellValidating was an inherently "while-editing" event.
Edit
The validation takes place in a validator class the top end of which looks like this:
public void ValidateCell(string tableName, DataGridView dataGrid, DataGridViewCellValidatingEventArgs e, ColumnCatalogue columnCatalogue)
{
if (!(dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null ||
dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == DBNull.Value ||
string.IsNullOrWhiteSpace(dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()))
)
{
ColumnDetails columnDetails = columnCatalogue.GetColumnDetails(tableName, dataGrid.Columns[e.ColumnIndex].Name);
switch (columnDetails.DataType)
{
case "currency":
this.ValidateCurrency(dataGrid, columnDetails, e);
break;
case "date":
this.ValidateDate(dataGrid, columnDetails, e);
break;
case "email":
this.ValidateEmail(dataGrid, columnDetails, e);
break;
case "int":
this.ValidateInt(dataGrid, columnDetails, e);
break;
case "phone":
this.ValidatePhone(dataGrid, columnDetails, e);
break;
case "postcode":
this.ValidatePostcode(dataGrid, columnDetails, e);
break;
default:
break;
}
}
}