Validating DataGrid bound to Datatable

Posted by: firelex on 7 January 2021, 9:02 pm EST

  • Posted 7 January 2021, 9:02 pm EST

    I have a DataTable filled from database.

    OleDbDataReader _r;
    DataTabel _dt = new DataTable();

    I bind this DataTable to my DataGrid in code behind.

    _grd.AutoGenerateColums = true;
    _grd.ItemsSource = _dt.DefaultView;

    The question is: how can I implement validation for data editing?

    I've tried to apply this post, but in my version of DataGrid there is not property

    Installed ComponentOne sample in the directory DataGridSamples uses model-side validation and I couldn't apply it as well.

    Any help is appreciated.
  • Replied 10 January 2021, 5:01 pm EST


    You can use ValidationRule for validating DataGrid bound with DataTable. You can create your validation rules by inheriting the ValidationRule class and then adding them to the Binding's ValidationRules collection as follows:

    column.Binding.ValidationRules.Add(new AgeValidationRule()
    Min = 18,
    Max = 45,

    Please refer to the attached sample for full implementation. (see

  • Replied 11 January 2021, 10:07 pm EST

    Thanks kartik.sagar! Your example works great.

    But there is one problem about your example: if I add an new row, enter only the name and leave age empty, no validation takes place: i.e.
    doesn't work. How can I fix this?
  • Replied 12 January 2021, 5:49 pm EST


    You can validate the new row using DataGrid's CommittingNewRow event and prevent it from being added to the DataGrid until it contains the valid data.

    Please refer the same from the attached modified sample. (see

  • Replied 13 January 2021, 11:46 pm EST

    Thanks kartik.sagar! Your example helped me much.

    However I have one more question.
    - CommittingNewRow event is triggered
    - error object is added to the Errors-Collection of NewRow
    - if I start editing the Age-Cell in the new row, the red message "Please enter an age..." is not shown. I can see it if the cursor is over the "!" in the first column, but not near the edited cell.

    How can I fix this?
  • Replied 14 January 2021, 5:17 pm EST


    ValidationRule shows validation message when the cell value is changed. Therefore, we have to type something in the cell in order to execute the validation logic and show the validation message.

    However, if you want to show the validation message when the cell comes in edit mode then you can use the DataRow's SetColumnError method to set the validation error message as follows:

    rowView.Row.SetColumnError("Age", error);

    Just in case required you can also use C1ValidationBehavior for highlighting the row containing errors as follows:

    <c1:C1DataGrid x:Name="dataGrid" AutoGeneratingColumn="dataGrid_AutoGeneratingColumn">

    Please refer to the same from the attached sample. (see

  • Replied 14 January 2021, 5:18 pm EST
  • Replied 15 January 2021, 1:38 am EST

    Thank you very much, Kartik! It's just what I needed!

    Is there any possibility to show the validation message when a combobox cell comes in edit mode?
    Using SetColumnError didn't do it. The cell gets red border, but no message is shown.

  • Replied 17 January 2021, 4:29 pm EST


    This seems like a bug so I have escalated it to the devs and will revert back to you when there is any update. [Internal Tracking Id - C1XAML-26806]

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels