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

Data binding error when adding children to new parents.

2 Answers 73 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Craig
Top achievements
Rank 1
Craig asked on 07 Feb 2013, 06:38 PM
Hi
I am having difficulty with databinding and child objects with the RadGridview control.
I have a custom object that exposes two properties; a string and a collection (List<>) of child objects. The child objects only expose a single string property.
If I load a number of instances of the custom object each with children, into an Observable collection, the databinding seems to work and I can add new children to the existing parents without any problem.
However, if I add a new parent, when I attempt to add a new child there are databinding errors stating the property name cannot be found and the child is not retained in the child table in the grid control. (BindingExpression path error: 'FloorNo' property not found on 'object' ''Object').
I'm using the MVVM pattern so I am trying to define the hierarchy in the XAML which I've listed below.
Note: Buildings is the observable collection of the custom object Building.
BuildingNo is the string property of Building and Floors is the List<Floor> property where Floor is the child object. FloorNo is the string property of Floor.
Please can you suggest what I need to do to correct or improve the XAML to enable the creation of new children in new parents?
Thanks
Craig
<telerik:RadGridView HorizontalAlignment="Left" Margin="378,0,0,262" Name="buildingsGridView" VerticalAlignment="Bottom" Width="193" Height="197"
                     ItemsSource="{Binding Buildings, Mode=TwoWay}" AutoGenerateColumns="False" ShowGroupPanel="False" ShowInsertRow="True"
                     RowHeight="23" RowIndicatorVisibility="Collapsed" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                     CanUserFreezeColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="True" SelectedItem="{Binding SelectedBuilding}">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding BuildingNo}" Header="Building No." IsFilterable="False" IsGroupable="False" IsReorderable="False" IsResizable="False" ShowDistinctFilters="False" Width="*"/>
    </telerik:RadGridView.Columns>
    <telerik:RadGridView.ChildTableDefinitions>
        <telerik:GridViewTableDefinition>
            <telerik:GridViewTableDefinition.Relation>
                <telerik:PropertyRelation ParentPropertyName="Floors" />
            </telerik:GridViewTableDefinition.Relation>
        </telerik:GridViewTableDefinition>
    </telerik:RadGridView.ChildTableDefinitions>
    <telerik:RadGridView.HierarchyChildTemplate>
        <DataTemplate>
            <telerik:RadGridView ItemsSource="{Binding Floors, Mode=TwoWay}" Name="floorsGrid" ShowGroupPanel="False" AutoGenerateColumns="False">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding FloorNo}" Header="Floor No." IsFilterable="False" IsGroupable="False" IsReorderable="False" IsResizable="False" ShowDistinctFilters="False" Width="*" />
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
        </DataTemplate>
    </telerik:RadGridView.HierarchyChildTemplate>
</telerik:RadGridView>

2 Answers, 1 is accepted

Sort by
0
Craig
Top achievements
Rank 1
answered on 08 Feb 2013, 10:38 AM
I found a solution. It was due to the class definition. The class now initialises the List<> collection in the constructor. Children can now be added to new parents.
0
Yoan
Telerik team
answered on 08 Feb 2013, 12:01 PM
Hi Craig,


Thank you for the update. I am glad to hear that the issue is now resolved.


All the best,
Yoan
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Craig
Top achievements
Rank 1
Answers by
Craig
Top achievements
Rank 1
Yoan
Telerik team
Share this question
or