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

Exception when trying to view child row in hierarchical grid

3 Answers 79 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Karl B
Top achievements
Rank 1
Karl B asked on 05 Aug 2015, 06:57 PM

I'm trying to figure out how to programmatically set up a hierarchical grid bound to a collection of objects. When I run the code shown below I get an exception when trying to expand the parent rows

In the form code behind-

public Form1()
    {
    InitializeComponent();
 
    List<Parent> parents = new List<Parent> ();
 
    parents.Add(CreateParent("Joe"));
    parents.Add(CreateParent("Bob"));
 
 
    this.parentBindingSource.DataSource = parents;
 
    }
 
private static Parent CreateParent (string parentName)
    {   
    var parent = new Parent () {ParentName = parentName};
 
    String childName = String.Format ("Child of {0}", parentName);
    parent.Children.Add(new Child() { Age = 2, ParentName = parentName, ChildName = "Child" });
    return parent;
    }

 Data class

public class Parent
    {
    private List<Child> m_children = new List<Child> ();
    public String ParentName { get; set; }
    public MyEnum FirstSecond { get; set; }
 
    public List<Child> Children { get { return m_children; } private set { m_children = value; } }
    }
 
public class Child
    {
    public String ChildName { get; set; }
    public String ParentName { get; set; }
    public MyEnum FirstSecond { get; set; }
    public int Age { get; set; }
    }
 
public enum MyEnum
    {
    First,
    Second
    }

3 Answers, 1 is accepted

Sort by
0
Karl B
Top achievements
Rank 1
answered on 05 Aug 2015, 06:59 PM

Sorry that code behind code was incorrect.

 Here is the correct code behind

public Form2()
    {
    InitializeComponent();
 
    List<Parent> parents = new List<Parent>();
    parents.Add(CreateParent("Joe"));
    parents.Add(CreateParent("Bob"));
 
    GridViewTemplate childTemplate = CreateChildTemplate ();
    GridViewRelation relation = new GridViewRelation(this.radGridView1.MasterTemplate, childTemplate);
    relation.ChildColumnNames.Add("ParentName");
    this.radGridView1.Relations.Add(relation);
 
    this.radGridView1.DataSource = parents;
    }
 
private GridViewTemplate CreateChildTemplate ()
    {
    GridViewTemplate childTemplate = new GridViewTemplate();
    this.radGridView1.Templates.Add(childTemplate);
    GridViewTextBoxColumn column = new GridViewTextBoxColumn("ChildName");
    childTemplate.Columns.Add(column);
    childTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
    return childTemplate;
    }
 
private static Parent CreateParent(string parentName)
    {
    var parent = new Parent() { ParentName = parentName };
    String childName = String.Format("Child of {0}", parentName);
    parent.Children.Add(new Child() { Age = 2, ParentName = parentName, ChildName = childName });
    return parent;
    }

0
Karl B
Top achievements
Rank 1
answered on 05 Aug 2015, 08:52 PM

I've discovered part of my problem-

1) needed to add relation.ParentColumnNames.Add("ParentName"); to avoid the exception

2) childTemplate.DataSource needs to be bound to a flat list of all children

 

Is there a better way to do step 2?  I would like to be able to tell the grid to load the nested Child list of each Parent class.

0
Accepted
Stefan
Telerik team
answered on 06 Aug 2015, 07:41 AM
Hello Karl,

Thank you for writing.

You can use the AutoGenerateHierarchy to setup the hierarchy that you need:
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
 
    List<Parent> parents = new List<Parent>();
    parents.Add(CreateParent("Joe"));
    parents.Add(CreateParent("Bob"));
  
    this.radGridView1.DataSource = parents;
    this.radGridView1.AutoGenerateHierarchy = true;
}
 
private static Parent CreateParent(string parentName)
{
    var parent = new Parent() { ParentName = parentName };
    String childName = String.Format("Child of {0}", parentName);
    parent.Children.Add(new Child() { Age = 2, ParentName = parentName, ChildName = childName });
    return parent;
}
 
public class Parent
{
    private List<Child> m_children = new List<Child>();
    public String ParentName { get; set; }
    public MyEnum1 FirstSecond { get; set; }
 
    public List<Child> Children { get { return m_children; } private set { m_children = value; } }
}
 
public class Child
{
    public String ChildName { get; set; }
    public String ParentName { get; set; }
    public MyEnum1 FirstSecond { get; set; }
    public int Age { get; set; }
}

Or to get it working in your case, you do not need the ParentColumnNames, but to set the ChildColumnNames to "Children" as this is the name of the field in the Parent object, which holds the child objects:
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
 
    List<Parent> parents = new List<Parent>();
    parents.Add(CreateParent("Joe"));
    parents.Add(CreateParent("Bob"));
 
    GridViewTemplate childTemplate = CreateChildTemplate();
    GridViewRelation relation = new GridViewRelation(this.radGridView1.MasterTemplate, childTemplate);
    relation.ChildColumnNames.Add("Children");
    this.radGridView1.Relations.Add(relation);
 
    this.radGridView1.DataSource = parents;
}
 
private static Parent CreateParent(string parentName)
{
    var parent = new Parent() { ParentName = parentName };
    String childName = String.Format("Child of {0}", parentName);
    parent.Children.Add(new Child() { Age = 2, ParentName = parentName, ChildName = childName });
    return parent;
}
 
public class Parent
{
    private List<Child> m_children = new List<Child>();
    public String ParentName { get; set; }
    public MyEnum1 FirstSecond { get; set; }
 
    public List<Child> Children { get { return m_children; } private set { m_children = value; } }
}
 
public class Child
{
    public String ChildName { get; set; }
    public String ParentName { get; set; }
    public MyEnum1 FirstSecond { get; set; }
    public int Age { get; set; }
}
 
private GridViewTemplate CreateChildTemplate()
{
    GridViewTemplate childTemplate = new GridViewTemplate();
    this.radGridView1.Templates.Add(childTemplate);
    GridViewTextBoxColumn column = new GridViewTextBoxColumn("ChildName");
    childTemplate.Columns.Add(column);
    childTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
    return childTemplate;
}
 
public enum MyEnum1
{
    First,
    Second
}

I hope that you find this information useful. Should you have any other questions, do not hesitate to contact us.

Regards,
Stefan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Karl B
Top achievements
Rank 1
Answers by
Karl B
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or