Skip to main content

Internal ErrorInfo Support

  • 6 minutes to read

The GridControl introduces APIs to display validation errors for the focused row and its individual cells. Use these APIs to indicate invalid user input.

  • Invalid cells display an error icon.
  • Invalid rows display an icon in the row indicator panel (GridView) or the card caption (CardView).

    GridView CardView
    Validation and Error Indication - WinForms GridView, DevExpress Validation and Error Indication - WinForms CardView, DevExpress
  • Predefined error icons include:
    • ErrorType.Critical
    • ErrorType.Information
    • ErrorType.Warning
  • You can also assign custom icons.

See the following help topic for more information on row validation and error indication: Manage User Input.

Set Errors

Use the ColumnView.SetColumnError method to indicate errors in the focused row.

Validate user input in the ValidatingEditor or ValidateRow event. If input is invalid, call the SetColumnError method to display feedback.

The following code snippet displays an error icon if the “Start Date” is later that the “End Date”:

gridView1.ValidateRow += (sender, e) => {
    DateTime startDate = (DateTime)gridView1.GetRowCellValue(e.RowHandle, "StartDate");
    DateTime endDate = (DateTime)gridView1.GetRowCellValue(e.RowHandle, "EndDate");

    // Validates the row data and sets the error if validation fails.
    if (startDate > endDate) {
        e.Valid = false;
        gridView1.SetColumnError(
            gridView1.Columns["StartDate"],
            "The Start Date cannot be later than the End Date. Please correct the value.",
            ErrorType.Critical);
        gridView1.SetColumnError(
            gridView1.Columns["EndDate"],
            "The End Date cannot be earlier than the Start Date. Please correct the value.",
            ErrorType.Critical);
    }
    else {
        // Clear errors if validation passes.
        gridView1.ClearColumnErrors();
    }
};

gridView1.InvalidRowException += (sender, e) => {
    e.ExceptionMode = ExceptionMode.NoAction;
};

The following code snippet sets an error for the entire row:

void gridView1_ValidateRow(object sender, ValidateRowEventArgs e) {
    if (((MyRecord)e.Row).FirstName == "" || ((MyRecord)e.Row).LastName == "") {
        gridView1.SetColumnError(
            null,
            "The First Name and Last Name cannot be empty. Please correct the value.",
            ErrorType.Critical);
        e.Valid = false;
    }
}

Obtain and Clear Errors

Use the following methods to obtain and clear errors:

Method Name Description
GetColumnError Returns the error description for the specified column.
GetColumnErrorType Returns the error type (ErrorType) for the specified column.
HasColumnErrors Returns true if an error exists in the focused row or its cells.
ClearColumnErrors() Clears all errors in the focused row and its cells.

ErrorInfo for External Data Sources

The Grid Control’s error API applies only to the focused row.

If the view is bound to a DataTable or DataView, use standard .NET APIs:

Note

The GridControl automatically displays error icons for cells marked with DataRow.SetColumnError.

IDataErrorInfo and IDXDataErrorInfo Support

To support validation errors in custom data sources, implement one of the following interfaces:

The following example uses the DXErrorProvider component and implements the IDXDataErrorInfo interface in a business object to automatically indicate invalid values and display text-based feedback (tooltips).

Indicate Invalid Values with IDXDataErrorInfo - WinForms Data Grid Validation

using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors.DXErrorProvider;

namespace DXApplication {
    public partial class Form1 : DevExpress.XtraEditors.XtraForm {
        DXErrorProvider errorProvider;
        public Form1() {
            InitializeComponent();
            // Initializes a data source and binds it to the grid control.
            List<MyRecord> records = MyRecord.InitData();
            gridControl1.DataSource = records;
            // Initializes the DXErrorProvider component that handles errors.
            errorProvider = new DXErrorProvider(this);
            // Binds the error provider to the data source to track errors.
            errorProvider.DataSource = records;
        }
    }

    public class MyRecord : IDXDataErrorInfo {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public void GetPropertyError(string propertyName, ErrorInfo info) {
            if(propertyName == "FirstName" && FirstName == "" ||
                propertyName == "LastName" && LastName == "")
                info.ErrorText = string.Format("The {0} field cannot be empty", propertyName);
        }
        public void GetError(ErrorInfo info) { }
        static public List<MyRecord> InitData() {
            return new List<MyRecord>() {
                new MyRecord(){ FirstName = "Mike", LastName = "Brown" },
                new MyRecord(){ FirstName = "Sandra", LastName = "Smith"},
                new MyRecord(){ FirstName = "Andy", LastName = "Muller" }
            };
        }
    }
}

View Example: Validate rows using IDXDataErrorInfo

See the following help topics for more information:

Custom Error Visualization

To customize error presentation:

  • Handle the CustomDrawColumnHeader event to draw custom content in headers.
  • Use appearance settings to highlight invalid cells or rows.

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.

ColumnView_ValidateRow

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;
}
See Also