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();
    ...
    _dt.Load(_r);


    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 https://www.grapecity.com/blogs/validation-using-idataerroinfo-in-wpf-c1datagrid, but in my version of DataGrid there is not property
    e.Row.ValidationResults
    .

    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

    Hi,

    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 DataGridValidation.zip)

    Regards,
    Kartik

    DataGridValidation.zip
  • 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.
    e.Row.Validate();
    doesn't work. How can I fix this?
  • Replied 12 January 2021, 5:49 pm EST

    Hi,

    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 DataGridValidation_Mod.zip)

    Regards,
    Kartik

    DataGridValidation_Mod.zip
  • Replied 13 January 2021, 11:46 pm EST

    Thanks kartik.sagar! Your example helped me much.

    However I have one more question.
    Scenario:
    - 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

    Hi,

    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">
    <c1:C1ValidationBehavior.ValidationBehavior>
    <c1:C1ValidationBehavior></c1:C1ValidationBehavior>
    </c1:C1ValidationBehavior.ValidationBehavior>
    </c1:C1DataGrid>

    Please refer to the same from the attached sample. (see DataGridValidation_Mod2.zip)

    Regards,
    Kartik
  • Replied 14 January 2021, 5:18 pm EST

    DataGridValidation_Mod2.zip
  • 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.


    Regards,
    firelex
  • Replied 17 January 2021, 4:29 pm EST

    Hi,

    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]

    Regards,
    Kartik
Need extra support?

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

Learn More

Forum Channels