RadGridView with Hierarchy in Winforms

4 posts, 0 answers
  1. Marco
    Marco avatar
    17 posts
    Member since:
    Jun 2018

    Posted 18 Sep 2018 Link to this post

    First, I'm new to Telerik Controls.

    I'm trying to show a simple hierarchical class (some Properties and a List<T> with sub entries) in a RadGridView. I figured out I can use "AutoGenerateHierarchy" which works so far. But I'm having a real hard time to make it look how I need it. Especially for the sub entries. What ever I change in the Property Builder on "Template1" doesn't seem to have any effect. Also does it display a + even if the entry doesn't have sub entries.

    I would appreciated if you could help me out with this...

  2. Marco
    Marco avatar
    17 posts
    Member since:
    Jun 2018

    Posted 19 Sep 2018 Link to this post

    I could figure it out myself... I've set the DataSource on the Control instead of the dedicated BindingSource...
  3. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3575 posts

    Posted 19 Sep 2018 Link to this post

    Hello, Marco,  

    When you set the AutoGenerateHierarchy property to true, RadGridView generates automatically the child template when setting the DataSource property. If you create the template in advance and then specify the DataSource at run time for example, the child template will be recreated and your customizations will be lost. I would recommend you to customize the child template programmatically after the grid is populated with data. You can find below a sample code snippet:
     
    public RadForm1()
    {
        InitializeComponent();
     
        BindingList<Item> items = new BindingList<Item>();
        for (int i = 0; i < 5; i++)
        {
            BindingList<SubItem> subItems = new BindingList<SubItem>();
            for (int j = 0; j < 3; j++)
            {
                subItems.Add(new SubItem(j,"SubItem" + i + "." + j));
            }
            items.Add(new Item(i,"Item" + i, subItems));
        }
     
        this.radGridView1.AutoGenerateHierarchy = true;
        this.radGridView1.DataSource = items;
        this.radGridView1.MasterTemplate.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
     
        //customize the child template programmatically
        this.radGridView1.MasterTemplate.Templates[0].AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
        this.radGridView1.MasterTemplate.Templates[0].AllowAddNewRow = false;
        this.radGridView1.MasterTemplate.Templates[0].EnableFiltering = true;
    }
     
    public class Item
    {
        public int Id { get; set; }
     
        public string Title { get; set; }
     
        public BindingList<SubItem> Items { get; set; }
     
        public Item(int id, string title, BindingList<SubItem> items)
        {
            this.Id = id;
            this.Title = title;
            this.Items = items;
        }
    }
     
    public class SubItem
    {
        public int Id { get; set; }
     
        public string Description { get; set; }
     
        public SubItem(int id, string description)
        {
            this.Id = id;
            this.Description = description;
        }
    }
     
    I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

    Regards,
    Dess
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  4. Marco
    Marco avatar
    17 posts
    Member since:
    Jun 2018

    Posted 19 Sep 2018 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    Hi Dess,

    Thank you, I did set the data on the control itself rather then the BindingSource. After changing that it worked. But I'll keep your hint in mind.

    Thank You

Back to Top