Datagrid problem: how handle datatable constraint exception? RRS feed

  • Question

  • I bind a datatable with a PK to datagrid. Now if I edit the PK column and set a value that already exists, and hit ENTER, I will get a red alert at front of the row. That is okay. But if I hit ENTER again, the red alert disappears and the row tries to commits and things are getting weird.

    1. I get a ConstraintException not handled by datagrid, but no error UI is shown at all. I can only see the exception in debug pane.

    2. The arrangment of some rows becomes strange:

    What should I do to avoid this weirdness?

    Ignorance is blissful

    • Edited by Neo the 1 Sunday, September 22, 2019 7:11 AM
    Sunday, September 22, 2019 7:10 AM

All replies

  • The usual approach is to use an auto generated primary key and don't let the users edit it.

    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Sunday, September 22, 2019 9:39 AM
  • The screenshot is taken from my simple test app. In my real world app, the key is unique. Even if I use auto generated PK, I still need to create an unique key constraint and hence the problem still exists.

    Ignorance is blissful

    Sunday, September 22, 2019 12:34 PM
  • Okay, after some search and tests, I work around the problem by implementing a custom ValidationRule:

    public class MyRule : ValidationRule
            public override ValidationResult Validate(object value, CultureInfo cultureInfo)
                var dr = ((value as BindingGroup).Items[0] as DataRowView).Row;
                var v = dr["k"] as string;
                if (string.IsNullOrEmpty(v))
                    return new ValidationResult(false, "Key cannot be empty");
                if (dr.Table.Rows.Find(v) != null)
                    return new ValidationResult(false, "Duplicate key entered");
                return ValidationResult.ValidResult;

    But this is just a workaround. There are two more questions:

    1. The framework should provide a mechanism to handle/respond to underlying data exceptions.

    2. How do I display custom error UI in case of validation failures (in stead of the default read alert icon)?

    Ignorance is blissful

    Monday, September 23, 2019 2:16 PM
  • Hi Neo the 1,

    > How do I display custom error UI in case of validation failures (in stead of the default read alert icon)?

    You can use Validation error messages which often displayed the in  three mechanisms: ErrorTemplates, ContentPresenters, Error Barsm to show the custom error.

    Example for using ErrorTemplate(please refer to How to: Implement Binding Validation for more details):

    <TextBox Name="textBox1" Width="50" FontSize="15"

             Validation.ErrorTemplate="{StaticResource validationTemplate}"

             Style="{StaticResource textBoxInError}"

             Grid.Row="1" Grid.Column="1" Margin="2">


                    <Binding Path="Age" Source="{StaticResource ods}"

                 UpdateSourceTrigger="PropertyChanged" >


                            <c:AgeRangeRule Min="21" Max="130"/>





    There is a sample about validation in WPF MVVM projects and detailed description of three mechanisms' verification.

    Best regards

    Daisy  Tian

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 26, 2019 1:11 AM