Data binding error when adding children to new parents.

3 posts, 0 answers
  1. Craig
    Craig avatar
    113 posts
    Member since:
    Nov 2011

    Posted 07 Feb 2013 Link to this post

    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. Craig
    Craig avatar
    113 posts
    Member since:
    Nov 2011

    Posted 08 Feb 2013 Link to this post

    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.
  3. Yoan
    Admin
    Yoan avatar
    1182 posts

    Posted 08 Feb 2013 Link to this post

    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.

Back to Top