This is a migrated thread and some comments may be shown as answers.

Display entity framework exceptions on binding in GridView

2 Answers 49 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Davidm
Top achievements
Rank 1
Davidm asked on 27 Jan 2012, 08:06 PM
I want to use RadGridView to display missing relations in a database using the Entity framework. I have defined several columns with path bindings to subtables. (eg. Table1.ID) When the datagrid is loading such a column with a 1 to 1 relation the entity framework throws an exception if the related row does not exist in the database, but the RadGridView does not display this error. (I tried several binding settings and manually added an ExceptionValidationRule and set the ValidateOnTargedUpdated = true, but the errors don't show up in the datagrid) The ultimate solution would be if I could display an error message like "Related row in Table XXX and ID 123 not found" in the tooltip of the grid cell.

The only way the error gets displayed is to write a validator which e.g. checks for null values and then to enter the cell edit mode. Then the error gets displayed. But I never got the entity framework exception to display in the datagrid.

Another approach is to write a DataTemplate for every column which triggers the Validation.HasError event. Using this method the errors get displayed when the column is bound, but this does also not work for entity framework exceptions and needs a lot of xaml code. What is the best solution to get a good error message without to write a lot of xaml code?

2 Answers, 1 is accepted

Sort by
0
Davidm
Top achievements
Rank 1
answered on 27 Jan 2012, 08:44 PM
Update: It seams that something has changed in the latest internal build of the RadGridView. The following example displays the errors correcly when using RadGridView Version 2011.3.1116.40, but not when using version 2011.3.1323.40:

<telerik:RadGridView Name="RadGridView1" telerik:StyleManager.Theme="Expression_Dark" ItemsSource="{Binding ItemsSource}" >
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn>
            <telerik:GridViewDataColumn.DataMemberBinding>
                <Binding Path="Table1.ID" Mode="TwoWay">
                    <Binding.ValidationRules>
                        <my:TestValidation ValidatesOnTargetUpdated="True" />
                    </Binding.ValidationRules>
                </Binding>
            </telerik:GridViewDataColumn.DataMemberBinding>
            <telerik:GridViewDataColumn.CellStyle>
                <Style TargetType="{x:Type telerik:GridViewCell}">
                    <Style.Triggers>
                        <Trigger Property="Validation.HasError" Value="true">
                            <Setter Property="ToolTip"
                                Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                                Path=(Validation.Errors)[0].ErrorContent}"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </telerik:GridViewDataColumn.CellStyle>
        </telerik:GridViewDataColumn>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>
0
Yordanka
Telerik team
answered on 01 Feb 2012, 05:21 PM
Hi,

Thank you for the sample. I've managed to reproduce the problem. Indeed we've changed this behavior a little bit, but I think that the change is for good. All you have to do is to delete your custom tooltip (see ValidationError.png for the result). 

However if you insist adding your custom tooltip you will have to customized it a little bit: 

<Grid>
        <Grid.Resources>
            <Style TargetType="ToolTip">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ToolTip">
                            <ItemsControl ItemsSource="{TemplateBinding Content}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Margin="8,4,8,4" MaxWidth="250" Foreground="White" Text="{Binding}" TextWrapping="Wrap"/>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                            </ItemsControl>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Grid.Resources>        
 
        <telerik:RadGridView Name="radGridView1" telerik:StyleManager.Theme="Expression_Dark" ItemsSource="{Binding ItemsSource}" AutoGenerateColumns="False">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn>
                    <telerik:GridViewDataColumn.DataMemberBinding>
                        <Binding Path="Table1.ID" UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <local:TestValidation ValidatesOnTargetUpdated="True" />
                            </Binding.ValidationRules>
                        </Binding>
                    </telerik:GridViewDataColumn.DataMemberBinding>
                    <telerik:GridViewDataColumn.CellStyle>
                        <Style TargetType="{x:Type telerik:GridViewCell}">
                            <Style.Triggers>
                                <Trigger Property="HasValidationErrors" Value="true">
                                    <Setter Property="ToolTip"
                                            Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Errors}"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </telerik:GridViewDataColumn.CellStyle>
                </telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>

You will be able to bind to GridViewCell.HasValidationErrors property (which will return errors for all nested elements in case of CellTemplate usage) and display all errors from GridViewCell.Errors property. Unfortunately Errors property is of type IEnumerable, so you will have to change ToolTip template also. (ValidationError1.png for the result).

P.S. We will introduce it with the next internal build and with the upcoming official release. 
 
Regards,
Yordanka
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Davidm
Top achievements
Rank 1
Answers by
Davidm
Top achievements
Rank 1
Yordanka
Telerik team
Share this question
or