creating GridViewTemplate dynamically in winforms

5 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;
                radGridView1.MasterTemplate.Templates.Add(firstChildtemplate);

                GridViewRelation relation = new GridViewRelation(radGridView1.MasterTemplate);
                relation.ChildTemplate = firstChildtemplate;
                relation.RelationName = "HeaderDetails";
                relation.ParentColumnNames.Add("HeaderID");
                relation.ChildColumnNames.Add("ParentHeaderID");
                radGridView1.Relations.Add(relation);


                GridViewTemplate secondChildtemplate = new GridViewTemplate();
                secondChildtemplate.DataSource = dt2;
                firstChildtemplate.Templates.Add(secondChildtemplate);

                GridViewRelation relation2 = new GridViewRelation(firstChildtemplate);
                relation2.ChildTemplate = secondChildtemplate;
                relation2.RelationName = "CAPFieldDetails";
                relation2.ParentColumnNames.Add("HeaderID");
                relation2.ChildColumnNames.Add("ParentHeaderID");
                radGridView1.Relations.Add(relation2);
  2. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3955 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()
    {
        InitializeComponent();
     
        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;
            parentTemplate.Templates.Add(firstChildtemplate);
     
            GridViewRelation relation = new GridViewRelation(parentTemplate);
            relation.ChildTemplate = firstChildtemplate;
            relation.RelationName = "MasterParent";
            relation.ParentColumnNames.Add("Id");
            relation.ChildColumnNames.Add("ParentId");
            radGridView1.Relations.Add(relation);
     
            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.

    Regards,
    Desislava
    Telerik
     
    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

    Thanks
  4. Gh Reza
    Gh Reza avatar
    12 posts
    Member since:
    Jul 2011

    Posted 29 Mar Link to this post

    Hi dear !

    How i can change my radGridView Templates at run time ?

     

  5. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3955 posts

    Posted 30 Mar Link to this post

    Hi, Reza,

    The RadGridView.Templates collection gives you access to the child hierarchical level. Thus, you can manipulate any settings of the specific template.

    However, if you are still experiencing any further difficulties, it would be greatly appreciated if you can specify what is the exact goal that you are trying to achieve. Thus, we would get better understanding of the precise case and think about a suitable solution.

    I hope this information helps. 

    Regards,
    Dess | Tech Support Engineer, Sr.
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
Back to Top