Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want datagrid to continue Adding or Editing DataGrid row even if row has error validation rule

this is sample in order to clarify the idea
<DataGrid Name="Dgrid"  AutoGenerateColumns="False" ItemsSource="{Binding Request.Obligations}" CanUserAddRows="True">


            <DataGrid.Columns>

                <DataGridTextColumn Binding="{Binding Id}" />
                <DataGridTextColumn Binding="{Binding Name}"/>
        <DataGridTextColumn Binding="{Binding Age}"/>

           </DataGrid.Columns>


            <DataGrid.RowValidationRules>
                <validationRules:ٌRowValidationRule ValidatesOnTargetUpdated="True" ValidationStep="UpdatedValue"  />
            </DataGrid.RowValidationRules>

            <DataGrid.RowValidationErrorTemplate>
                <ControlTemplate>
                    <!--<Grid Margin="0,-2,0,-2"
            ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}},Path=(Validation.Errors)[0].ErrorContent}">-->
                    <Grid Margin="0,-2,0,-2">
                        <Grid.ToolTip>
                            <MultiBinding Converter="{StaticResource DataGridRowMultiConverter}">
                                <Binding Path="(Validation.Errors)[0].ErrorContent"
                                         RelativeSource="{RelativeSource Mode=FindAncestor,
                                                AncestorType=DataGridRow}" />
                                <Binding Path="DataContext"
                                         RelativeSource="{RelativeSource Mode=FindAncestor,
                                                AncestorType=Window}" />
                            </MultiBinding>
                        </Grid.ToolTip>
                        <Ellipse StrokeThickness="0" Fill="Red" Width="{TemplateBinding FontSize}"
                                 Height="{TemplateBinding FontSize}" />
                        <TextBlock Text="!" FontSize="{TemplateBinding FontSize}" FontWeight="Bold" Foreground="White"
                                   HorizontalAlignment="Center" />
                    </Grid>
                </ControlTemplate>
            </DataGrid.RowValidationErrorTemplate>

        </DataGrid>

If a user entered a new row with invalid data, he will not be able to add a new row and the new row will be missing and the existing data can not be edited.

In short, I want to stop the behavior of RowValidationRules and make its jop to tell the user that he has entered invalid data without stoping him.

After search i found this code:
C#
protected override void OnCanExecuteBeginEdit(System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            var hasCellValidationError = false;
            var hasRowValidationError = false;
            const BindingFlags bindingFlags =
                BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Instance;
            var cellError = this.GetType().BaseType.GetProperty("HasCellValidationError", bindingFlags);
            var rowError = this.GetType().BaseType.GetProperty("HasRowValidationError", bindingFlags);

            if (cellError != null)
                hasCellValidationError = (bool)cellError.GetValue(this, null);
            if (rowError != null)
                hasRowValidationError = (bool)rowError.GetValue(this, null);

            base.OnCanExecuteBeginEdit(e);
            if ((!e.CanExecute && hasCellValidationError) || (!e.CanExecute && hasRowValidationError))
            {
                e.CanExecute = true;
                e.Handled = true;
            }
        }

but I found that I can edit rows but can't add more rows

What I have tried:

continue Adding or Editing DataGrid row even if row has error validation rule
Posted
Updated 15-May-17 9:22am
v3

1 solution

Quote:
Continue adding or editing datagrid row even if row has error validation rule

Why do you want a validation rules if you don't want to have to respect it ?

The easiest solution is not validation rule.
 
Share this answer
 
Comments
moh_mou 16-May-17 3:59am    
In short. In order to use RowValidationErrorTemplate to tell the user that he has entered invalid data without stoping him and to highlight the row or cell

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900