1
votes

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;
            } 
        }
    }
1
Can you post the code for the full validation method?Justin Russo
@JustinRusso Now addedifinlay

1 Answers

3
votes

As the data are not yet validated they can't be in the Cell's Value field.

Instead the input, as entered by the user, is both in the EditedFormattedValue field of the Cell..:

dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue

..and the FormattedValue field of the parameter e..:

e.FormattedValue

Note that both are of type object, already, after (usually) being entered as a string into the TextBox eding control.

Also note that for the existing and filled cells, testing their Value field is actually testing the old, unedited values! So you don't have a problem only with Value for the inserted Rows, you have it for all rows!