HOME PAGE | DOWNLOAD | TUTORIALS | XtraReports
Devexpress

Sunday, July 22, 2012

How to: Validate Data Entered by End-Users

The following example prohibits invalid "colBudget" column cell value assignment. The cell value should be grater than zero and less than 1,000,000. The BaseView.ValidatingEditor event is handled to check the entered value's validity. The BaseView.InvalidValueException event is handled to display an exception message box if invalid cell value assignment occurs. In this instance, the GridView.HideEditor method is called to discard the changes made and to destroy the cell's editor.

 

C#

using DevExpress.XtraEditors.Controls;

// ...

private void gridView1_ValidatingEditor(object sender, BaseContainerValidateEditorEventArgs e) {

   if (gridView1.FocusedColumn.Name != "colBudget") return;

   if ((Convert.ToInt32(e.Value) < 0) || (Convert.ToInt32(e.Value) > 1000000))

      e.Valid = false;

}

 

private void gridView1_InvalidValueException(object sender, InvalidValueExceptionEventArgs e) {

    e.ExceptionMode = ExceptionMode.DisplayError;

    e.WindowCaption = "Input Error";

    e.ErrorText = "The value should be greater than 0 and less than 1,000,000";

 

    // Destroying the editor and discarding the changes made within the edited cell

    gridView1.HideEditor();

}

 

VB

Imports DevExpress.XtraEditors.Controls
' ...
Private Sub GridView1_ValidatingEditor(ByVal sender As Object, _
  ByVal e As BaseContainerValidateEditorEventArgs) Handles GridView1.ValidatingEditor
   If GridView1.FocusedColumn.Name <> "colBudget" Then Exit Sub
   If (Convert.ToInt32(e.Value) < 0) Or (Convert.ToInt32(e.Value) > 1000000) Then
      e.Valid = False
   End If
End Sub
 
Private Sub GridView1_InvalidValueException(ByVal sender As Object, _
  ByVal e As ValidatingEditorEventArgs) Handles GridView1.InvalidValueException
   e.ExceptionMode = ExceptionMode.DisplayError
   e.WindowCaption = "Input Error"
   e.WindowText = "The value should be greater than 0 and less than 1,000,000"
 
   ' Destroying the editor and discarding the changes made within the edited cell
   GridView1.HideEditor()
End Sub

 

No comments:

Post a Comment