Back to Devexpress

Internal ErrorInfo Support

windowsforms-750-controls-and-libraries-data-grid-data-editing-and-validation-errorinfo-support-internal-errorinfo-support.md

latest13.1 KB
Original Source

Internal ErrorInfo Support

  • Jul 22, 2025
  • 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).

  • 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”:

csharp
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:

csharp
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 NameDescription
GetColumnErrorReturns the error description for the specified column.
GetColumnErrorTypeReturns the error type (ErrorType) for the specified column.
HasColumnErrorsReturns 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).

csharp
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" }
            };
        }
    }
}
vb
Imports DevExpress.XtraEditors.DXErrorProvider

Namespace DXApplication50
  Partial Public Class Form1
    Inherits DevExpress.XtraEditors.XtraForm

    Private errorProvider As DXErrorProvider
    Public Sub New()
      InitializeComponent()
      Dim records As List(Of MyRecord) = MyRecord.InitData()
      gridControl1.DataSource = records
      errorProvider = New DXErrorProvider(Me)
      errorProvider.DataSource = records
    End Sub
  End Class
  Public Class MyRecord
    Inherits IDXDataErrorInfo

    Public Property FirstName() As String
    Public Property LastName() As String
    Public Sub GetPropertyError(ByVal propertyName As String, ByVal info As ErrorInfo)
      If propertyName = "FirstName" AndAlso FirstName = "" OrElse propertyName = "LastName" AndAlso LastName = "" Then
        info.ErrorText = String.Format("The {0} field cannot be empty", propertyName)
      End If
    End Sub
    Public Sub GetError(ByVal info As ErrorInfo)
    End Sub
    Public Shared Function InitData() As List(Of MyRecord)
      Return New List(Of MyRecord)() From {
        New MyRecord() With {.FirstName = "Mike", .LastName = "Brown"},
        New MyRecord() With {.FirstName = "Sandra", .LastName = "Smith"},
        New MyRecord() With {.FirstName = "Andy", .LastName = "Muller"}
      }
    End Function
  End Class
End Namespace

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.

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

Edit Data. Create Cell Editors. Validate User Input

Error Notification Support for Data Sources

Tutorial: Data Input Validation