Back to Devexpress

How to: Validate Modified Rows

windowsforms-3067-controls-and-libraries-data-grid-examples-data-editing-how-to-validate-modified-rows.md

latest3.5 KB
Original Source

How to: Validate Modified Rows

  • Nov 13, 2018
  • 2 minutes to read

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