Back to Devexpress

RepositoryItem.Validating Event

windowsforms-devexpress-dot-xtraeditors-dot-repository-dot-repositoryitem-20ed25d2.md

latest6.4 KB
Original Source

RepositoryItem.Validating Event

Allows you to specify whether the edit value is valid. This event does not occur if the editor’s CausesValidation property is disabled.

Namespace : DevExpress.XtraEditors.Repository

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Events")]
public event CancelEventHandler Validating
vb
<DXCategory("Events")>
Public Event Validating As CancelEventHandler

Event Data

The Validating event's data class is CancelEventArgs. The following properties provide information specific to this event:

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

Remarks

The Validating event fires when a user accepts the edited value (i.e., the editor is about start the validation). This can take place in two cases:

Handle the Validating event to test the edit value against your acceptance criteria. If the entered value is not acceptable, you can set the event’s Cancel parameter to true to keep the focus on the current cell. Thus, you will force users to correct the edit value. For example, you can check whether the entered text contains only appropriate characters or if a numeric value falls into a predefined range, etc.

If you need to provide a validation error text for an in-place editor within a data-aware control (Data Grid, Tree List, etc.), do not modify the BaseEdit.ErrorText property on the editor’s Validating event, Instead, handle the parent data-aware control’s ValidatingEditor event. Refer to the See Also section below for a list of these events.

For detailed information on how to implement validation, see the Validation help topic.

The code sample below illustrates how to validate the price value.

csharp
RepositoryItemTextEdit edit = new RepositoryItemTextEdit();
edit.Validating += Edit_Validating;
gridView1.Columns["productPrice"].ColumnEdit = edit;
gridControl1.RepositoryItems.Add(edit);

private void Edit_Validating(object sender, CancelEventArgs e)
{
    var edit = sender as TextEdit;
    decimal r = 0.0M;
    if (!decimal.TryParse(edit.EditValue.ToString(), out r))
        e.Cancel = true;
}
vb
Private edit As New RepositoryItemTextEdit()
Private edit.Validating += AddressOf Edit_Validating
Private gridView1.Columns("productPrice").ColumnEdit = edit
gridControl1.RepositoryItems.Add(edit)

private void Edit_Validating(Object sender, CancelEventArgs e)
    Dim edit = TryCast(sender, TextEdit)
    Dim r As Decimal = 0.0D
    If Not Decimal.TryParse(edit.EditValue.ToString(), r) Then
        e.Cancel = True
    End If

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the Validating event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-scheduler-localizer-translate-ui/CS/SchedulerLocalizerExample/OutlookAppointmentForm.cs#L262

csharp
this.edtEndTime.InvalidValue += new InvalidValueExceptionEventHandler(OnEdtEndTimeInvalidValue);
this.riDuration.Validating += new CancelEventHandler(OnCbReminderValidating);
this.edtStartDate.Validating += new CancelEventHandler(OnEdtStartDateValidating);

winforms-scheduler-localizer-translate-ui/VB/SchedulerLocalizationExample_VB/OutlookAppointmentForm.vb#L295

vb
AddHandler Me.edtEndTime.InvalidValue, AddressOf OnEdtEndTimeInvalidValue
AddHandler Me.riDuration.Validating, AddressOf OnCbReminderValidating
AddHandler Me.edtStartDate.Validating, AddressOf OnEdtStartDateValidating

See Also

OldEditValue

EditValue

ErrorText

ValidateOnEnterKey

BaseView.ValidatingEditor

PivotGridControl.ValidatingEditor

TreeList.ValidatingEditor

VGridControlBase.ValidatingEditor

RepositoryItem Class

RepositoryItem Members

DevExpress.XtraEditors.Repository Namespace