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
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.
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
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.
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
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.
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;
}
}
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
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
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/.
Great! Thanks a lot Dess. I really appreaciate.
Have a good day.
Louise