Bind grid to a model with Navigation property?

1 Answer 114 Views
Grid
Brian
Top achievements
Rank 1
Brian asked on 16 Sep 2022, 05:26 PM

I've got an EF Core model that represents a one-to-many relationship. There's a navigation property on the parent that holds a collection of child items. What I'd like to do is bind this to a hierarchical grid and have the child items display underneath the parent items.

The example for a hierarchical grid shows separate data sources for the parent and child tables. Since I've already retrieved this data, why not just display it in that form?

I have a datasource defined which has an include statement. I tried to adapt the example code and have the same datasource for both grids, using syntax like "Client.Order" in the child table, but it doesn't like that.

Many thanks for any suggestions.

1 Answer, 1 is accepted

Sort by
0
Stoyan
Telerik team
answered on 21 Sep 2022, 12:26 PM

Hi Brian,

The Grid uses flat data and allows you to define one Grid as a child of another Grid.

To achieve the requirement at hand I recommend you to use the TreeList Component. It can bind to hierarchical data and display it in gridlike table structure.

For the TreeList to interpret the Json it receives as hierarchical data the Model that is bound to the Component must have a field that is configured as a ParentId. In the TreeList Demo this is the ReportsTo field:

DataSource(dataSource => dataSource
        .Read(read => read.Action("All", "EmployeeDirectory"))
        .ServerOperation(false)
        .Model(m => {
            m.Id(f => f.EmployeeId);
            m.ParentId(f => f.ReportsTo);
            m.Expanded(true);
            m.Field(f => f.FirstName);
            m.Field(f => f.LastName);
            m.Field(f => f.ReportsTo);
        })
    )

I hope the information above is useful. Please let me know, if further questions on the topic arise.

Regards,
Stoyan
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Brian
Top achievements
Rank 1
commented on 22 Sep 2022, 08:34 PM

Thank you for the response. Unfortunately a tree control will not work for my purposes, since the parent and child tables have non-homogenous data. I.E. there are fields in the child that aren't in the parent, and vice-versa.

It's irritating because setting up a dbcontext with the related data all nicely arranged hierarchically, but I have to jump through hoops to get the grid to display it. Does datasource even support hierarchical data?

Stoyan
Telerik team
commented on 27 Sep 2022, 01:18 PM

Hi Brian,

The DataSources of the TreeList and TreeView Components are converted to a Hierarchical DataSource behind the scenes. However they expect homogenous data as you have noticed. 

One possible approach to achieve the requirement at hand as I understand it is to define one large ViewModel that combines the different properties of the hierarchical data. This ViewModel should only have a common Id and ParentId. Then you'll be able to represent the hierarchical data in a single Component.

The drawback of this approach is that some columns will always be empty.

Alternatively, the best approach would be to utilize Hierarchical Grids.

Tags
Grid
Asked by
Brian
Top achievements
Rank 1
Answers by
Stoyan
Telerik team
Share this question
or