Exception when trying to view child row in hierarchical grid

4 posts, 1 answers
  1. Karl
    Karl avatar
    35 posts
    Member since:
    Dec 2013

    Posted 05 Aug 2015 Link to this post

    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
        }

  2. Karl
    Karl avatar
    35 posts
    Member since:
    Dec 2013

    Posted 05 Aug 2015 in reply to Karl Link to this post

    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;
        }

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Karl
    Karl avatar
    35 posts
    Member since:
    Dec 2013

    Posted 05 Aug 2015 Link to this post

    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.

  5. Answer
    Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 06 Aug 2015 Link to this post

    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
Back to Top