Breyta

Deila með


DataGridViewCellValidatingEventArgs Class

Definition

Provides data for the CellValidating event of a DataGridView control.

public ref class DataGridViewCellValidatingEventArgs : System::ComponentModel::CancelEventArgs
public class DataGridViewCellValidatingEventArgs : System.ComponentModel.CancelEventArgs
type DataGridViewCellValidatingEventArgs = class
    inherit CancelEventArgs
Public Class DataGridViewCellValidatingEventArgs
Inherits CancelEventArgs
Inheritance
DataGridViewCellValidatingEventArgs

Examples

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.

void VirtualConnector::dataGridView1_CellValidating
    (Object^ sender, DataGridViewCellValidatingEventArgs^ e)
{
    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 (!Int32::TryParse(e->FormattedValue->ToString(), 
        newInteger) || (newInteger < 0))
    {
        e->Cancel = true;
    }
}
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";
    }
}
Private Sub dataGridView1_CellValidating(ByVal sender As Object, _
    ByVal e _
    As DataGridViewCellValidatingEventArgs) _
    Handles dataGridView1.CellValidating

    Me.dataGridView1.Rows(e.RowIndex).ErrorText = ""
    Dim newInteger As Integer

    ' 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 Then Return
    If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) _
        OrElse newInteger < 0 Then

        e.Cancel = True
        Me.dataGridView1.Rows(e.RowIndex).ErrorText = "the value must be a non-negative integer"

    End If
End Sub

Remarks

The DataGridView.CellValidating event lets you cancel changes to the current cell when the new value is not valid. Use the FormattedValue property to determine the current value. To determine the state of the current cell, use the RowIndex and ColumnIndex properties to access the cell through the DataGridView.Rows collection. To cancel the change, set the Cancel property to true.

When this event is canceled in data-bound mode, the new value is not pushed to the underlying data source. When this event is canceled in virtual mode, the DataGridView.CellValuePushed event will not be raised.

Properties

Cancel

Gets or sets a value indicating whether the event should be canceled.

(Inherited from CancelEventArgs)
ColumnIndex

Gets the column index of the cell that needs to be validated.

FormattedValue

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

RowIndex

Gets the row index of the cell that needs to be validated.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also