Manual Object-Relational Hierarchy Not Quite Working

2 Answers 48 Views
GridView
Michael
Top achievements
Rank 1
Veteran
Michael asked on 10 Feb 2022, 05:26 PM

I have a grid whose datasource is a list of DocumentGroup objects, each of which has a property "Others" of type IEnumerable<Document>.

I wrote the following:

        GridViewTemplate childTemplate = new GridViewTemplate();
        grdDocuments.Templates.Add(childTemplate);
        childTemplate.Columns.Add(new GridViewTextBoxColumn(nameof(Document.Docname)));
        childTemplate.Columns.Add(new GridViewTextBoxColumn(nameof(Document.Filepath)));
        GridViewRelation relation = new GridViewRelation(grdDocuments.MasterTemplate, childTemplate);
        relation.ChildColumnNames.Add(nameof(CompanyTb.DocumentGroup.Others));
        childTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        grdDocuments.Relations.Add(relation);
        
        grdDocuments.Fill(Data.DocumentGroups);

(I'm not showing the whole setup of grdDocuments.)

This basically works, in that it shows a row for each DocumentGroup with the data in their columns, plus an icon to the left of each allowing the user to open the associated sub-grid of Documents.  Clicking on that icon gives me the correct number of rows under each DocumentGroup and each row has two columns (one for the Docname property and one for the Filepath property).

The only problem is that all the cells in all the Document rows are empty, even though there's data in the objects behind them.

What am I missing?

2 Answers, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 11 Feb 2022, 03:05 PM

Hello Michael,

Thank you for the provided code snippet.

At first sight, it seems that the set-up is correct. However, I could be missing something from your implementation. Could it be possible to share a sample project which demonstrates this behavior? This way I can take a closer look and debug it on my side.

I am looking forward to your reply.

Regards,
Dinko
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Samuel
Top achievements
Rank 1
commented on 07 Mar 2022, 02:27 PM

As I wrote you some time ago -- when last I had time for this -- it's not practical for me to construct a minimal project that demonstrates the problem.  I'm fine with doing my share of the debugging under your guidance, though.

So one thing that occurs to me is that in the examples I've seen in your forum the relation has its ChildColumnNames and ParentColumnNames set to cconnect the child grid to the parent grid.  I don't have that.

If that indeed is the problem then I have two comments:

  1. This seems like an arbitrary and unhelpful restriction.  The child table can be populated from a collection contained in the parent row, where the objects in the collection don't have back-pointers to their containing object.  My data, for example, doesn't come straight from a table; these are POCOs.
  2. I did indicate the property of the parent object that is the collection of child objects and that worked to the extent that the correct number of child rows is generated for each parent row.  How is that happening if the relation is unaware of how they're connected?

I look forward to your response.

0
Dinko | Tech Support Engineer
Telerik team
answered on 10 Mar 2022, 09:30 AM

Hello Michael,

I will start with that I am assuming that you are setting the hierarchy in RadGridView bound mode. When we have RadGridView in Hierarchical mode, in order for the child table to be able to match the parent row child collection, the ChildColumnNames and ParentColumnNames properties need to be set. Otherwise, the relation won't know which columns to use to match the child template source. My guess here is that the experienced behavior is related to the fact that you are building a relationship with a column that is not available in your child template. If that column is not necessary you can hide it but it needs to be part of the template. 

What I can suggest here first is to check the pass strings as parameters to the columns. Check nameof(Document.Docname) and nameof(Document.Filepath)). One of the child column field names needs to match a column field name from its parent grid. Also, the matching columns need to have equal types otherwise the relation won't work. At last but not least, you need to set the ChildColumnNames and ParentColumnNames properties of the relation to point out which child column to use to match the parent one. 

For your convenience, I have prepared a sample project to demonstrate the above settings. When you run the project, expand the row, you can observe how the second column cell is empty. This is because the fieldname property of the second column is not set. You can debug this in your project.

As a side note, if you get an exception when you try to run the example, saying that the Nwind.mdb cannot be found, navigate to the project folder. Copy the Nwind.mdb file and paste it inside the bin\debug folder.

Regards,
Dinko
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/.

Tags
GridView
Asked by
Michael
Top achievements
Rank 1
Veteran
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or