DataGridViewCellValidatingEventArgs.FormattedValue Property

 

Gets the formatted contents of the cell that needs to be validated.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public object FormattedValue { get; }

Property Value

Type: System.Object

A reference to the formatted value.

The text entered by the user through the user interface (UI) becomes the FormattedValue property value. This is the value that you can validate before it is parsed into the cell Value property value.

The following code example handles the CellValidating event to ensure that only positive integers are entered by the user. This example is part of a larger example available in the VirtualMode reference topic.

private void dataGridView1_CellValidating(object sender,
    DataGridViewCellValidatingEventArgs e)
{
    dataGridView1.Rows[e.RowIndex].ErrorText = "";
    int newInteger;

    // Don't try to validate the 'new row' until finished 
    // editing since there
    // is not any point in validating its initial value.
    if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
    if (!int.TryParse(e.FormattedValue.ToString(),
        out newInteger) || newInteger < 0)
    {
        e.Cancel = true;
        dataGridView1.Rows[e.RowIndex].ErrorText = "the value must be a non-negative integer";
    }
}

.NET Framework
Available since 2.0
Return to top
Show: