RadGrid / group / hierarchical relation.

1 Answer 67 Views
GridView
Vincent
Top achievements
Rank 1
Iron
Iron
Vincent asked on 02 Jun 2021, 03:56 PM

Hi !

excuse my speed message, it's end of job day.

i'm using designer to find the good solution before make it by codehind.

i've got a relation from 2 class (P(parent) C(child) by idP)

i want group parent by "programme" , hide group possibilities and panel.

Result wanted :

PROG 1

> P1-AA

>> a

>>aa

>> aaa

>P1-BB ....

but i've got a bad result see attachment.

 

i've got 2 class for sample source:



 List<PClass> P = new List<PClass> { new PClass() {programme="PROG 1" , idP = 1, LibP = "P1-AA" },
                                                new PClass() {programme="PROG 1", idP = 2, LibP = "P1-BB" },
                                                new PClass() {programme="PROG 2", idP = 3, LibP = "P2-CC" },
                                                new PClass() {programme="PROG 2", idP = 4, LibP = "P2-DD" },
                                                new PClass() {programme="PROG 2", idP = 5, LibP = "P2-EE" }
                                              };



            List<Cclass> C = new List<Cclass> { new Cclass() { idC = 1, idP = 1, LibC = "a" },
                                                new Cclass() { idC = 2, idP = 1, LibC = "aa" },
                                                new Cclass() { idC = 3, idP = 1, LibC = "aaa" },
                                                new Cclass() { idC = 4, idP = 2, LibC = "p1-b" },
                                                new Cclass() { idC = 5, idP = 2, LibC = "p1-bb" },
                                                new Cclass() { idC = 6, idP = 3, LibC = "p2-c" },
                                                new Cclass() { idC = 7, idP = 4, LibC = "p2-d" },
                                                new Cclass() { idC = 8, idP = 4, LibC = "p2-dd" },
                                                new Cclass() { idC = 9, idP = 4, LibC = "p2-ddd" },
                                                new Cclass() { idC = 10, idP = 5, LibC = "p2-e" },
                                                new Cclass() { idC = 11, idP = 5, LibC = "p2-ee" }
                                                };

rg.DataSource = P;
gridViewTemplate1.DataSource = C;

Thanks for your help.

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 03 Jun 2021, 11:30 AM

Hello, Vincent,

If I understand you correctly you would like to have a hierarchy grid for two related data objects (P(parent) and C(child) related by a common idP property). Then, you want to group the grid by 'programme' in order to be able to see the results only applicable for the separate programme (Prog 1 and Prog 2).

In order to create a hierarchical view for two related data objects, you need to do is to set up a parent GridViewTemplate and a child GridViewTemplate. Then add the child GridViewTemplate in the parent GridViewTemplate.Templates collection. Then it is necessary to create a GridViewRelation specifying the parent/child template and add this relation in the RadGridView.Relations collection. Please refer to the following help article which is quite useful on this topic: https://docs.telerik.com/devtools/winforms/controls/gridview/hierarchical-grid/binding-to-hierarchical-data-programmatically 

I am not familiar what is the exact set-up that you have on your end for the hierarchy grid but considering the picture, it seems there is something wrong. For your reference, I have prepared a sample project that illustrates the steps you should follow to achieve the desired result. Please refer to the following code snippet:

 public partial class RadForm1 : Telerik.WinControls.UI.RadForm
 {
     public RadForm1()
     {
         InitializeComponent();

         List<PClass> P = new List<PClass> { new PClass() {programme="PROG 1" , idP = 1, LibP = "P1-AA" },
                                             new PClass() {programme="PROG 1", idP = 2, LibP = "P1-BB" },
                                             new PClass() {programme="PROG 2", idP = 3, LibP = "P2-CC" },
                                             new PClass() {programme="PROG 2", idP = 4, LibP = "P2-DD" },
                                             new PClass() {programme="PROG 2", idP = 5, LibP = "P2-EE" }
                                           };

         List<Cclass> C = new List<Cclass> { new Cclass() { idC = 1, idP = 1, LibC = "a" },
                                             new Cclass() { idC = 2, idP = 1, LibC = "aa" },
                                             new Cclass() { idC = 3, idP = 1, LibC = "aaa" },
                                             new Cclass() { idC = 4, idP = 2, LibC = "p1-b" },
                                             new Cclass() { idC = 5, idP = 2, LibC = "p1-bb" },
                                             new Cclass() { idC = 6, idP = 3, LibC = "p2-c" },
                                             new Cclass() { idC = 7, idP = 4, LibC = "p2-d" },
                                             new Cclass() { idC = 8, idP = 4, LibC = "p2-dd" },
                                             new Cclass() { idC = 9, idP = 4, LibC = "p2-ddd" },
                                             new Cclass() { idC = 10, idP = 5, LibC = "p2-e" },
                                             new Cclass() { idC = 11, idP = 5, LibC = "p2-ee" }
                                             };

         this.radGridView1.DataSource = P;

         GridViewTemplate childTemplate = new GridViewTemplate();
         childTemplate.DataSource = C;
         radGridView1.Templates.Add(childTemplate);

         GridViewRelation relation = new GridViewRelation(radGridView1.MasterTemplate);
         relation.ChildTemplate = childTemplate;
         relation.RelationName = "MasterParent";
         relation.ParentColumnNames.Add("idP");
         relation.ChildColumnNames.Add("idP");
         radGridView1.Relations.Add(relation);

     }

 }
 public class PClass
 {
     public string programme { get; internal set; }
     public int idP { get; internal set; }
     public string LibP { get; internal set; }
 }

 public class Cclass
 {
     public int idC { get; internal set; }
     public int idP { get; internal set; }
     public string LibC { get; internal set; }
 }

The hierarchy grid looks as the following picture:

The following picture shows the results after the grid is grouped by 'programme' column:

I hope this helps. Should you have other questions do not hesitate to contact me. 

Regards,
Nadya
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.

Tags
GridView
Asked by
Vincent
Top achievements
Rank 1
Iron
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or