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

creating GridViewTemplate dynamically in winforms

4 Answers 441 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Vidhya
Top achievements
Rank 1
Vidhya asked on 02 Sep 2014, 08:11 AM
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);

4 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 04 Sep 2014, 03:00 PM
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.
 
0
Vidhya
Top achievements
Rank 1
answered on 16 Oct 2014, 04:32 AM
Thanks
0
Gh Reza
Top achievements
Rank 1
answered on 29 Mar 2020, 03:50 PM

Hi dear !

How i can change my radGridView Templates at run time ?

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 30 Mar 2020, 10:05 AM

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.
Tags
GridView
Asked by
Vidhya
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Vidhya
Top achievements
Rank 1
Gh Reza
Top achievements
Rank 1
Share this question
or