Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Thetype exposes the following members.
|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.|
|Equals(Object)||Determines whether the specified object is equal to the current object. (Inherited from Object.)|
|Finalize||Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (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.)|
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.
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 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