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

How to maintain multilevel hierarchy in GridView

3 Answers 267 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Manish Mishra
Top achievements
Rank 1
Manish Mishra asked on 13 Jul 2012, 05:12 PM
hello everyone..
am using telerik radGridView for windows forms. I need to have atleast of 3 levels of hierarchy in my grid. And, it is essential for me to follow "Load-on-Demand" method to bind the inner grids.
Currently am doing this:


   private void Form2_Load(object sender, EventArgs e)
        {
            this.radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;




            //First Child Template Begins


            GridViewTemplate firstLevelTemplate = new GridViewTemplate();
            firstLevelTemplate.Columns.Add("Name", "Name", "Name");
            firstLevelTemplate.Columns.Add("Owner", "Owner", "Owner");
            firstLevelTemplate.Columns.Add("Parent", "Parent", "Parent");


            //First Child Template Ends


            this.radGridView1.Templates.Add(firstLevelTemplate);
            this.radGridView1.Templates[0].HierarchyDataProvider = new GridViewEventDataProvider(firstLevelTemplate);
            this.radGridView1.RowSourceNeeded += new GridViewRowSourceNeededEventHandler(radGridView1_RowSourceNeeded);
        }




        void radGridView1_RowSourceNeeded(object sender, GridViewRowSourceNeededEventArgs e)
        {
            string serverInstance = e.ParentRow.Cells[0].Value.ToString();
            ServerConnection conn = new ServerConnection();
            conn.ServerInstance = serverInstance;
            Server srv = new Server(conn);
       
            DatabaseCollection DBs = srv.Databases;
            foreach (Database db in DBs)
            {
                GridViewRowInfo row = e.Template.Rows.NewRow();
                row.Cells[0].Value = db.Name;
                row.Cells[1].Value = db.Owner;
                row.Cells[2].Value = db.Parent.Name;


                e.SourceCollection.Add(row);
            }

        }


It  beautifully adds the two levels! I want a third level now. How to do that?? I tried adding GridViewTemplate to e.Template.Templates, but no luck..please help me..

3 Answers, 1 is accepted

Sort by
0
Accepted
Nikolay
Telerik team
answered on 16 Jul 2012, 11:59 AM
Hi Manish,

Thank you for the question.

In order to create the third level of hierarchy, you should create another template, add it to the Templates collection of the firstLevelTemplate and also create a new GridViewEventDataProvider for it. In short, you should add the following code snippet to your existing one in the Form2_Load method:

//Second Child Template Begins
GridViewTemplate secondLevelTemplate = new GridViewTemplate();
secondLevelTemplate.Columns.Add("Name", "Name", "Name");
secondLevelTemplate.Columns.Add("Owner", "Owner", "Owner");
secondLevelTemplate.Columns.Add("Parent", "Parent", "Parent");
//Second Child Template Ends
 
firstLevelTemplate.Templates.Add(secondLevelTemplate);
this.radGridView1.Templates[0].Templates[0].HierarchyDataProvider = new GridViewEventDataProvider(secondLevelTemplate);

I hope this helps.

Kind regards,
Nikolay
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Prasad
Top achievements
Rank 1
answered on 03 Jun 2014, 03:16 PM
Code mentioned in above thread looks a bit weird [sorry!]. What if I have a nested grid till nth level and want to load the data dynamically? do I need to keep to maintain template hierarchy using Templates[0].Templates[0].Templates[0].Templates[0] etc.

this.radGridView1.Templates[0].Templates[0].HierarchyDataProvider = new GridViewEventDataProvider(secondLevelTemplate);

My requirement is I need to load the data dynamically, have different child template structure at each level. Additionally I want to display expand / collapse sign. Kindly let me know how it can be done.

Thanks.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 06 Jun 2014, 09:42 AM
Hello Prasad,

Thank you for writing.

In order to construct a n-level hierarchy in the RadGridView, you should add a GridViewTemplate for each level. Afterwards, it is necessary to initialize the HierarchyDataProvider for the certain template as my colleague Nikolay demonstrated in the previous post. Thus, using the RadGridView.RowSourceNeeded event, you can load on demand the hierarchical data, considering the ParentRow.HierarchyLevel. Please refer to our Load-On-Demand Hierarchy, which is quite useful about configuring different child levels on the fly.

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