The ValidationError events are raised by the AddNew or EndEdit methods when the value of a cell or one of the cells in a DataRow or InsertionRow does not pass the validation process.
It is suggested that these events be handled since, by default, no exceptions will be thrown by Xceed Grid for WinForms if an invalid value is entered into a cell.
To subscribe to the ValidationError events, the following steps must be performed:
Obtain a reference to a Cell, DataRow or InsertionRow object.
Subscribe to the ValidationError event of the desired object using either the CellValidationErrorEventHandler or RowValidationErrorEventHandler delegate class.
Create a new method that will handle the events that are raised.
Place the desired code in the newly created event handler.
To subscribe to the ValidationError event, the following steps must be performed:
Obtain a reference to a Cell, DataRow or InsertionRow object.
Subscribe to the ValidationError event of the desired object using the AddHandler/AddressOf keywords.
Create a new method that will handle the events that are raised.
Place the desired code in the newly created event handler.
This example assumes that you are in a Windows application and that you are handling the ValidationError event of each cell in the DataRowTemplate (thus for every cell in the grid).
VB.NET |
Copy Code |
---|---|
Dim cell As DataCell For Each cell In GridControl1.DataRowTemplate.Cells AddHandler cell.ValidationError, AddressOf Me.cell_ValidationError Next Private Sub cell_ValidationError(ByVal sender As Object, ByVal e As CellValidationErrorEventArgs) Try MessageBox.Show(e.Value.ToString() + " is not a valid value for " + sender.FieldName) e.CancelEdit = False Catch exception As Exception MessageBox.Show( exception.ToString() ) End Try End Sub ' If you no longer wish to handle the ValidationError events that are raised, ' you can unsubscribe from the event notification by doing the following: RemoveHandler cell.ValidationError, AddressOf Me.cell_ValidationError |
C# |
Copy Code |
---|---|
foreach( DataCell cell in gridControl1.DataRowTemplates.Cells ) { cell.ValidationError += new ValidationErrorEventHandler( this.cell_ValidationError ); } private void cell_ValidationError( object sender, CellValidationErrorEventArgs e ) { try { MessageBox.Show( e.Value.ToString() + " is not a valid value for " + ( ( DataCell )sender ).FieldName ); e.CancelEdit = false; } catch( Exception exception ) { MessageBox.Show( exception.ToString() ); } } // If you no longer wish to handle the ValidationError events that are raised, // you can unsubscribe from the event notification by doing the following: cell.ValidationError -= new ValidationErrorEventHandler( this.cell_ValidationError ); |