creating GridViewTemplate dynamically in winforms

3 posts, 0 answers
  1. Vidhya
    Vidhya avatar
    3 posts
    Member since:
    Jun 2014

    Posted 02 Sep 2014 Link to this post

    How to create gridviewtemplate dynamically in winforms?

    The sample code below is with statically created gridviewtemplate, how to make it dynamically with 'n' number of gridviewtemplate from code?

                radGridView1.DataSource = dt;

                GridViewTemplate firstChildtemplate = new GridViewTemplate();
                firstChildtemplate.DataSource = dt1;

                GridViewRelation relation = new GridViewRelation(radGridView1.MasterTemplate);
                relation.ChildTemplate = firstChildtemplate;
                relation.RelationName = "HeaderDetails";

                GridViewTemplate secondChildtemplate = new GridViewTemplate();
                secondChildtemplate.DataSource = dt2;

                GridViewRelation relation2 = new GridViewRelation(firstChildtemplate);
                relation2.ChildTemplate = secondChildtemplate;
                relation2.RelationName = "CAPFieldDetails";
  2. Dess | Tech Support Engineer, Sr.
    Dess | Tech Support Engineer, Sr.  avatar
    3167 posts

    Posted 04 Sep 2014 Link to this post

    Hello Vidhya,

    Thank you for writing.

    In order to create a hierarchical view for two related data source objects (e.g DataTables connected via "Id" and "ParentId" columns), all you need to do is to set up a parent GridViewTemplate and a child GridViewTemplate. Then add the child GridViewTemplate in the parent GridViewTemplate.Templates collection. The last necessary thing is to create a GridViewRelation specifying the parent/child template and add this relation in the RadGridView.Relations collection. Our Binding to Hierarchical Data Programmatically help article is useful about this topic. Here is a sample code snippet, demonstrating how to set up a 10 level hierarchy:
    int level = 10;
    Random rand = new Random();
    int currentLevel = 0;
    public Form1()
        DataTable masterDt = new DataTable();
        masterDt.Columns.Add("Id", typeof(int));
        masterDt.Columns.Add("ParentId", typeof(int));
        masterDt.Columns.Add("Title", typeof(string));
        for (int j = 0; j < 2; j++)
            masterDt.Rows.Add(j, 0, "Parent." + currentLevel + "." + j);
        this.radGridView1.MasterTemplate.DataSource = masterDt;
        this.radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        CreateInnerTemplate(this.radGridView1.MasterTemplate, currentLevel);
    private void CreateInnerTemplate(GridViewTemplate parentTemplate, int currentLevel)
        if (currentLevel < level)
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("ParentId", typeof(int));
            dt.Columns.Add("Title", typeof(string));
            for (int i = 0; i < 2; i++)
                for (int j = 0; j < 10; j++)
                    dt.Rows.Add(j, i, "Child." + currentLevel + "." + j);
            GridViewTemplate firstChildtemplate = new GridViewTemplate();
            firstChildtemplate.DataSource = dt;
            firstChildtemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            GridViewRelation relation = new GridViewRelation(parentTemplate);
            relation.ChildTemplate = firstChildtemplate;
            relation.RelationName = "MasterParent";
            CreateInnerTemplate(firstChildtemplate, ++currentLevel);

    Note that this is just a sample which purpose is to demonstrate the approach and it may not cover all possible cases. Feel free to modify it on a way which suits your requirement best.
    I hope this information helps. Should you have further questions, I would be glad to help.

    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
  3. Vidhya
    Vidhya avatar
    3 posts
    Member since:
    Jun 2014

    Posted 15 Oct 2014 in reply to Dess | Tech Support Engineer, Sr. Link to this post

Back to Top