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

Binding to a Hierarchical custom object

1 Answer 25 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Saj
Top achievements
Rank 1
Saj asked on 11 Apr 2014, 02:36 PM
I'm having trouble binding a custom object
All the examples seem to be database/dataset based with separate collections for each dimension.

How would I get a simple hierarchical grid to work with something like below, if I wanted to bind to an object of type List<CarMaker> 

public class CarMaker
{
        public string CompanyName { get; set; }
        public List<CarModel> Models { get; set; }
}

public class CarModel
{
        public string ModelName { get; set; }
}

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 16 Apr 2014, 08:21 AM
Hello Luke,

Thank you for writing.

In order to achieve your requirement, please refer to the sample code below:
public Form1()
{
    InitializeComponent();
 
    List<CarMaker> carMakers = new List<CarMaker>();
    for (int i = 1; i < 5; i++)
    {
        List<CarModel> carModels = new List<CarModel>();
        for (int j = i * 100; j < i * 100 + 10; j++)
        {
            carModels.Add(new CarModel("Model" + j));
        }
        carMakers.Add(new CarMaker("CompanyName" + i,carModels));
    }
 
    this.radGridView1.DataSource = carMakers;
    this.radGridView1.Columns["Models"].IsVisible = false;
    this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
    GridViewTemplate childTemplate = new GridViewTemplate();
    childTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
    GridViewTextBoxColumn modelNameColumn = new GridViewTextBoxColumn("ModelName");
    childTemplate.Columns.Add(modelNameColumn);
 
    this.radGridView1.Templates.Add(childTemplate);
 
    childTemplate.HierarchyDataProvider = new GridViewEventDataProvider(childTemplate);
    this.radGridView1.RowSourceNeeded += radGridView1_RowSourceNeeded;
}
 
private void radGridView1_RowSourceNeeded(object sender, GridViewRowSourceNeededEventArgs e)
{
    CarMaker carMaker = e.ParentRow.DataBoundItem as CarMaker;
 
    foreach (CarModel carModel in carMaker.Models)
    {
        GridViewRowInfo row = e.Template.Rows.NewRow();
        row.Cells["ModelName"].Value = carModel.ModelName;
 
        e.SourceCollection.Add(row);
    }
}
 
public class CarMaker
{
    public string CompanyName { get; set; }
 
    public List<CarModel> Models { get; set; }
 
    public CarMaker(string companyName, List<CarModel> models)
    {
        this.CompanyName = companyName;
        this.Models = models;
    }
}
 
public class CarModel
{
    public string ModelName { get; set; }
 
    public CarModel(string modelName)
    {
        this.ModelName = modelName;
    }
}

Our Load-On-Demand Hierarchy help article is quite useful about the used approach.

Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

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