Back to Devexpress

ColumnView.ValidateRow Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-base-dot-columnview-c1c4ca4f.md

latest8.9 KB
Original Source

ColumnView.ValidateRow Event

Fires when a user edits row cell values and attempts to select another row. Handle this event to check whether these new values are valid, and if not, choose the required behavior (discard edits, show a warning message, ignore errors, or keep the focus on this row until a user enters valid values).

Namespace : DevExpress.XtraGrid.Views.Base

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Action")]
public event ValidateRowEventHandler ValidateRow
vb
<DXCategory("Action")>
Public Event ValidateRow As ValidateRowEventHandler

Event Data

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

PropertyDescription
ErrorTextGets or sets the error description.
RowGets the processed row. Inherited from RowObjectEventArgs.
RowHandleGets the row’s handle (position). For the ColumnView.RowUpdated event, this property specifies the previous handle (position) of the currently processed row. NewItemRowHandle value when a new row is added. Inherited from RowEventArgs.
ValidGets or sets whether the row validation succeeds.

Remarks

When a user modifies cell values and attempts to move focus to another row, the ValidateRow event fires. Handle this event to review all changes (use the ColumnView.GetRowCellValue and ColumnView.GetRowCellDisplayText method to do that). If any cell value is incorrect, set the e.Valid parameter to false. The following behavior depends on whether you have handled the ColumnView.InvalidRowException event, and if so, what value is assigned to its e.ExceptionMode parameter.

If the InvalidRowException event was not handled (or its ExceptionMode equals DisplayError), Data Grid keeps a user on this row and shows a notification message. The ValidateRowEventArgs.ErrorText parameter allows you to modify this message’s text. A user can click “No” to dismiss this warning and move to another row, but all edits made to the current row cells will be lost. Note that Data Grid can rollback cell values only when data source entities implement the IEditableObject interface.

Instead of validating an entire row, you can handle the BaseView.ValidatingEditor and BaseView.InvalidValueException events to validate every row cell separately.

See the Modify and Validate Cell Values and Internal ErrorInfo Support articles for more information on cell and row validation.

Example

The example below tracks changes made to the “Units In Stock” and “Units On Order” columns. If an edited record has its “In Stock” value less than “On Order”, this row is considered invalid. In this case the e.Valid parameter of the ColumnView.ValidateRow event is set to false.

The default warning message does not pop up since the ExceptionMode parameter of the ColumnView.InvalidRowException event is set to NoAction. Instead, the ColumnView.SetColumnError method displays error icons in both cells.

csharp
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Controls;

private void gridView1_ValidateRow(object sender, 
DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e) {
    GridView view = sender as GridView;
    GridColumn inStockCol = view.Columns["UnitsInStock"];
    GridColumn onOrderCol = view.Columns["UnitsOnOrder"];
    //Get the value of the first column
    Int16 inSt = (Int16)view.GetRowCellValue(e.RowHandle, inStockCol);
    //Get the value of the second column
    Int16 onOrd = (Int16)view.GetRowCellValue(e.RowHandle, onOrderCol);
    //Validity criterion
    if (inSt < onOrd) {
        e.Valid = false;
        //Set errors with specific descriptions for the columns
        view.SetColumnError(inStockCol, "The value must be greater than Units On Order");
        view.SetColumnError(onOrderCol, "The value must be less than Units In Stock");
    }
    if(e.Valid)
        view.ClearColumnErrors();
}

private void gridView1_InvalidRowException(object sender, 
DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e) {
    //Suppress displaying the error message box
    e.ExceptionMode = ExceptionMode.NoAction;
}
vb
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors.Controls

Private Sub GridView1_ValidateRow(ByVal sender As Object, _
ByVal e As DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs) _
Handles GridView1.ValidateRow
    Dim View As GridView = CType(sender, GridView)
    Dim inStockCol As GridColumn = View.Columns("UnitsInStock")
    Dim onOrderCol As GridColumn = View.Columns("UnitsOnOrder")
    'Get the value of the first column
    Dim inSt As Int16 = CType(View.GetRowCellValue(e.RowHandle, UnitsInStock), Int16)
    'Get the value of the second column
    Dim onOrd As Int16 = CType(View.GetRowCellValue(e.RowHandle, UnitsOnOrder), Int16)
    'Validity criterion
    If inSt < onOrd Then
        e.Valid = False
        'Set errors with specific descriptions for the columns
        View.SetColumnError(inStockCol, "The value must be greater than Units On Order")
        View.SetColumnError(onOrderCol, "The value must be less than Units In Stock")
    End If
    If e.Valid Then
        View.ClearColumnErrors()
    End If
End Sub

Private Sub GridView1_InvalidRowException(ByVal sender As Object, _
ByVal e As DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs) _
Handles GridView1.InvalidRowException
    'Suppress displaying the error message box
    e.ExceptionMode = ExceptionMode.NoAction
End Sub

See Also

FocusedRowHandle

InvalidRowException

SetColumnError

UpdateCurrentRow()

ValidatingEditor

AllowValidationErrors

Edit Data. Create Cell Editors. Validate User Input

ColumnView Class

ColumnView Members

DevExpress.XtraGrid.Views.Base Namespace