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

RadGridView with Hierarchy in Winforms

3 Answers 481 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Marco
Top achievements
Rank 1
Marco asked on 18 Sep 2018, 03:28 PM

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...

3 Answers, 1 is accepted

Sort by
0
Marco
Top achievements
Rank 1
answered on 19 Sep 2018, 07:40 AM
I could figure it out myself... I've set the DataSource on the Control instead of the dedicated BindingSource...
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 19 Sep 2018, 07:44 AM
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.
0
Marco
Top achievements
Rank 1
answered on 19 Sep 2018, 09:01 AM

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

Tags
GridView
Asked by
Marco
Top achievements
Rank 1
Answers by
Marco
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or