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

Iterating the child rows of each parent row in Hierarchical Grid

10 Answers 726 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Yael
Top achievements
Rank 1
Yael asked on 22 Sep 2008, 09:29 AM
Hi
I don't know how to iterate the child rows of each parent row in Hierarchical Grid.
I havn't found any example for this.

I am looking for something like:
foreach (parentrow in grid)
{
    ChildRowCollection = GetChildRow(parentrow)
    foreach (ChildRow in ChildRowCollection )
    {
        do something on the ChildRow
    }
}

Thanks in advance
Yael Kline

10 Answers, 1 is accepted

Sort by
0
Martin Vasilev
Telerik team
answered on 24 Sep 2008, 10:17 PM
Hi Yael,

Thank you for the question.

You could iterate the child rows by using the GetChildRows method of the child template. Please, review the code-block below as example:
 
private void IterateChildRows(GridViewDataRowInfo rowInfo) 
    GridViewRowInfo[] childRows =  
        this.radGridView1.MasterGridViewTemplate.ChildGridViewTemplates[0].GetChildRows(rowInfo); 
 
    for (int i = 0; i < childRows.Length; i++) 
    { 
        //do something with childRows[i] 
    } 

Hope this helps, if you have other questions, do not hesitate to contact me again.

Regards,
Martin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Yael
Top achievements
Rank 1
answered on 25 Sep 2008, 11:49 AM
Thanks
I tried the following code:
foreach(GridViewDataRowInfo rowInfo in grid.MasterGridViewInfo.Rows)
{
   GridViewRowInfo[] childRows =  
        this.grid.MasterGridViewTemplate.ChildGridViewTemplates[0].GetChildRows(rowInfo); 
  if (childRows.Length > 0)
{
    for (int i = 0; i < childRows.Length; i++) 
    {  

         if (!(childRows[i].Cells["checkbox"].Value is System.DBNull) 
                        && (bool)childRows[i].Cells["checkbox"].Value) 
                    { 
                        containsCheckedRow = true; 
                    } 
                    else 
                    { 
                        allChecked = false; 
                    }  
            } 
}
}

I can view the childRows array in the 'watch' screen and i see all the child rows of the parent row.
But I get exception 'SystemNullReference' on the row:
childRows[i].Cells["checkbox"].Value
Every thing in 'Cells' is: 'Object reference not set to an instance of an object'
So i can't use it...
Any idea?

Thanks yael
0
Martin Vasilev
Telerik team
answered on 26 Sep 2008, 06:23 PM
Hello Yael,

Thank you for getting back to me.

To access the child row cells you have to expand hierarchy before iterating. Here is the modified code:
 
private void IterateChildRows(GridViewDataRowInfo rowInfo) 
    bool expandedState = rowInfo.IsExpanded; 
    rowInfo.IsExpanded = true;  
 
    GridViewRowInfo[] childRows = 
        rowInfo.ViewTemplate.ChildGridViewTemplates[0].GetChildRows(rowInfo); 
 
    for (int i = 0; i < childRows.Length; i++) 
    { 
        //do something with childRows[i] 
        Console.WriteLine(childRows[i].Cells[0].Value); 
    } 
 
    rowInfo.IsExpanded = expandedState; 
 
    this.radGridView1.GridElement.Update(GridUINotifyAction.Reset); 

Please, let me know if this does not solve the issue.

Sincerely yours,
Martin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Yael
Top achievements
Rank 1
answered on 28 Sep 2008, 09:32 AM
Hi
The 'IsExpanded' did the trik.

I would like to know if there is some place in the documentation that give this information and an example?
The iterating child rows and the need to use 'IsExpanded' to be able to get the CELL information???

Thanks
Yael
0
Martin Vasilev
Telerik team
answered on 01 Oct 2008, 08:44 AM
Hello Yael,

Thank you for the question.

IsExpanded=true is needed, because RadGridView uses virtualization. Child cells do not get create before the child view is expanded.

I have created a KB Article about this case thanks to your feedback - I have updated your Telerik points about that. The information in the KB article will be included in the documentation in the next release:
http://www.telerik.com/support/kb/article/b454K-bbak-b454T-cta-b454c-cta.aspx

Contact me again, if you have other questions.

 
Greetings,
Martin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Freddie
Top achievements
Rank 1
answered on 03 Sep 2014, 09:30 PM
Hi, I use the following code to iterating the child rows but I cannot see those child rows that have been filtered out. Basically how can I iterating the child rows including those have been filtered out? 

foreach (GridViewRowInfo rowInfo in radGVTaskScheduler.Rows)
{
     GridViewHierarchyRowInfo hierarchyRow = rowInfo as GridViewHierarchyRowInfo;
     if (hierarchyRow != null)
     {
          //IterateChildRows(hierarchyRow);
          GridViewInfo currentView = hierarchyRow.ActiveView;
          foreach (GridViewInfo view in hierarchyRow.Views)
          {
               hierarchyRow.ActiveView = view;
               foreach (GridViewRowInfo row in hierarchyRow.ChildRows)
               {
                    //Console.WriteLine(row.Cells[2].Value);
                }
           }
           hierarchyRow.ActiveView = currentView;
     }
}
0
George
Telerik team
answered on 08 Sep 2014, 04:14 PM
Hello Freddie,

Thank you for replying.

Since the Rows collection contains all rows and the ChildRows collection contains the visible rows you can basically get all the rows that do not appear in the ChildRows collection. This can happen easily using the Except method:
private void Button_Click1352(object sender, EventArgs e)
{
    var rows = this.Grid.Rows;
    var childRows = this.Grid.ChildRows;
  
    var filteredRows = rows.Except(childRows).ToList();
}

I hope this helps.

Regards,
George
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
Louise
Top achievements
Rank 1
answered on 05 Mar 2021, 10:16 PM

 

I have an error when I use this code.  ChildGridViewTemplates[0] is not recognized. And if I put Templates[0] (to access the first child template), then it's GetChildRows that I have trouble with. I know this tread is old but I didn't find anything else. I'm using version 2021.1.223.40. What I want is to iterate each child templates. For each of them, I want to put the data in a DataTable in put that in a different dummy grid. How can I do that with the version 2021.1.223.40?

 

private void IterateChildRows(GridViewDataRowInfo rowInfo) 


    bool expandedState = rowInfo.IsExpanded; 
    rowInfo.IsExpanded = true;  

    GridViewRowInfo[] childRows = 
        rowInfo.ViewTemplate.ChildGridViewTemplates[0].GetChildRows(rowInfo); 

    for (int i = 0; i < childRows.Length; i++) 
    { 
        //do something with childRows[i] 
        Console.WriteLine(childRows[i].Cells[0].Value); 
    } 

    rowInfo.IsExpanded = expandedState; 

    this.radGridView1.GridElement.Update(GridUINotifyAction.Reset); 

 

Regards,

Louise

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 08 Mar 2021, 01:49 PM
Hello, Louise,

I can see that you are using Martin's solution which was posted a long time ago back in 2008. Since then, RadGridView was majorly refactored and indeed, the ChildGridViewTemplates property is not available for the ViewTemplate. Feel free to use the ViewTemplate.Templates collection instead. Each template offers the ChildRows collection where it stores the rows that it contains. 
        private void IterateChildRows(GridViewDataRowInfo rowInfo) 
        { 
            bool expandedState = rowInfo.IsExpanded; 
            rowInfo.IsExpanded = true;  

            for (int i = 0; i < rowInfo.ViewTemplate.Templates[0].ChildRows.Count; i++) 
            { 
                //do something with childRows[i] 
                Console.WriteLine(rowInfo.ViewTemplate.Templates[0].ChildRows[i].Cells[0].Value); 
            }

            rowInfo.IsExpanded = expandedState; 

            this.radGridView1.MasterTemplate.Refresh();
        }

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

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

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Louise
Top achievements
Rank 1
answered on 08 Mar 2021, 01:53 PM

Great! Thanks a lot Dess. I really appreaciate.

Have a good day.

Louise

Tags
GridView
Asked by
Yael
Top achievements
Rank 1
Answers by
Martin Vasilev
Telerik team
Yael
Top achievements
Rank 1
Freddie
Top achievements
Rank 1
George
Telerik team
Louise
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or