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

How to bind to Custom-Objects, where each object contains a list of customs objects?

1 Answer 74 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 04 Jun 2012, 02:50 PM
Hi!

I have a question: how can I bind a list of custom objects to a radgridview, where each custom object itself contains a list of custom objects.

Here is an example to get a clearer:
===========================

Custom Objects:

public class ArticleData()
{
    public string MonthName { get; set; }
    public decimal MonthValue { get; set; }
}

public class Article()
{
    public string ArticleNo { get; set; )
    public string ArticleName { get; set; )

    public List<ArticleData> ArticleDetails();

    public Article()
    {
        ArticleDetails = new List<ArticleData>();
    }
}

Filling with test data:

List<Article> ArticleList = new List<Article>();

Article a = new Article();
a.ArticleNo = "1000";
a.ArticleName = "Testarticle 1000";
a.ArticleDetails.add(new ArticleData { MonthName = "January", MonatValue = 10 });
a.ArticleDetails.add(new ArticleData { MonthName = "February", MonatValue = 5 });
...
ArticleList.add(a);

Article b = new Article();
b.ArticleNo = "2000";
b.ArticleName = "Testarticle 2000";
b.ArticleDetails.add(new ArticleData { MonthName = "January", MonatValue = 6 });
b.ArticleDetails.add(new ArticleData { MonthName = "February", MonatValue = 3 });
...
ArticleList.add(b);

and so on ...


DataBinding to Grid:

Imagine, I have a grid, with 14 columns: the first two columns should represent ArticleNo and ArticleName. The other columns should represent 12 months of a year with each MonthValue.

GridViewTextColumn colArticleNo = new GridViewTextBoxColumn();
colArticleNo.FieldName = "ArticleNo";
radgridview1.columns.add(colArticleNo);

GridViewTextColumn colArticleName = new GridViewTextBoxColumn();
colArticleName.FieldName = "ArticleName";
radgridview1.columns.add(colArticleName);

GridViewDecimalColumn colJanuary = new GridViewDecimalColumn();
colJanuary.FieldName = "Article.ArticleDetails.MonthValue";

GridViewDecimalColumn colFebruary = new GridViewDecimalColumn();
colFebruary.FieldName = "Article.ArticleDetails.MonthValue";

radgridview1.DataSource = ArticleList;

How can I achieve, that for the January column, the ArticleDetails[0].MonthValue is bound, for the February column ArticleDetails[1].MonthValue ist bound and so on??

Kind regards

Michael

1 Answer, 1 is accepted

Sort by
0
Ivan Todorov
Telerik team
answered on 06 Jun 2012, 09:48 AM
Hi Michael,

Thank you for your question.

RadGridView supports object-relational hierarchy mode which covers exactly this scenario. Here is how you can setup the grid to display your data:
public Form1()
{
    InitializeComponent();
    List<Article> ArticleList = new List<Article>();
    Article a = new Article();
    a.ArticleNo = "1000";
    a.ArticleName = "Testarticle 1000";
    a.ArticleDetails.Add(new ArticleData { MonthName = "January", MonthValue = 10 });
    a.ArticleDetails.Add(new ArticleData { MonthName = "February", MonthValue = 5 });
    ArticleList.Add(a);
    Article b = new Article();
    b.ArticleNo = "2000";
    b.ArticleName = "Testarticle 2000";
    b.ArticleDetails.Add(new ArticleData { MonthName = "January", MonthValue = 6 });
    b.ArticleDetails.Add(new ArticleData { MonthName = "February", MonthValue = 3 });
    ArticleList.Add(b);
       
    GridViewTemplate childTemplate = CreateChildTemplate();
 
    GridViewRelation relation = new GridViewRelation(this.radGridView1.MasterTemplate, childTemplate);
    relation.ChildColumnNames.Add("ArticleDetails");
    this.radGridView1.Relations.Add(relation);
    this.radGridView1.DataSource = ArticleList;
}
 
private GridViewTemplate CreateChildTemplate()
{
    GridViewTemplate childTemplate = new GridViewTemplate();
    this.radGridView1.Templates.Add(childTemplate);
    GridViewTextBoxColumn column = new GridViewTextBoxColumn("MonthName");
    childTemplate.Columns.Add(column);
    GridViewDecimalColumn decColumn = new GridViewDecimalColumn("MonthValue");
    childTemplate.Columns.Add(decColumn);
    childTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
    return childTemplate;
}

Additional details can be found in the following online documentation article: Object Relational Hierarchy Mode.

I hope this will help you. In case you have any additional questions, do not hesitate to ask.

Regards,
Ivan Todorov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
Michael
Top achievements
Rank 1
Answers by
Ivan Todorov
Telerik team
Share this question
or