This is a migrated thread and some comments may be shown as answers.
Allow only one row to expand in a hierarchical RadGridView
4 Answers 14 Views
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
Veteran
James asked on 31 Dec 2020, 04:32 PM
How do I allow only one row in a hierarchical RadGridView to expand at a time?

4 Answers, 1 is accepted

Sort by
0
Nadya
Telerik team
answered on 04 Jan 2021, 02:18 PM

Hello, James,

RadGridView offers ChildViewExpanding event that triggers when the child view is about to be expanded or collapsed. This event can be canceled. You can use it in order to achieve your requirement and allow only one row to expand. An example is demonstrated below:

 this.radGridView1.ChildViewExpanding += this.RadGridView1_ChildViewExpanding;
 private void RadGridView1_ChildViewExpanding(object sender, ChildViewExpandingEventArgs e)
 {
     if (e.ParentRow.Index != 1)
     {
         e.Cancel = true;
     }
 }

I hope this helps. Should you have any other questions please let me know.

Regards,
Nadya
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
James
Top achievements
Rank 1
Veteran
answered on 04 Jan 2021, 08:35 PM

Thanks for the reply, but this is exactly what I am looking for. I don't want to stop the user from expanding a row. What I would like to happen is for all rows except for the currently expanding row to collapse. 

Any thoughts on how to achieve this?

Thanks again in advance,

0
James
Top achievements
Rank 1
Veteran
answered on 05 Jan 2021, 08:48 AM

Edit from my last post. What I meant to say is:

Thanks for the reply, but this is NOT exactly what I am looking for. I don't want to stop the user from expanding a row. What I would like to happen is for all rows except for the currently expanding row to collapse. 
Any thoughts on how to achieve this?
Thanks again in advance,

0
Nadya
Telerik team
answered on 06 Jan 2021, 08:27 AM

Hello, James,

Thank you for providing additional information. If I understand you correctly you would like to be able to expand all rows, however, when one row is expanded the rest gets collapsed automatically. In this case, you should iterate the GridViewHierarchyRowInfos and collapse all except the current one. You can refer to the following help article: https://docs.telerik.com/devtools/winforms/controls/gridview/hierarchical-grid/how-to/iterating-the-child-rows-collection-of-a-chosen-parent-row-in-hierarchy-radgridview 

I prepared an example:

private void RadGridView1_ChildViewExpanded(object sender, ChildViewExpandedEventArgs e)
{
    if (e.ChildViewInfo != null)
    {
        CollapseRowsExceptOne();
    }
}
void CollapseRowsExceptOne()
{
    foreach (GridViewRowInfo row in radGridView1.Rows)
    {
        GridViewHierarchyRowInfo hierarchyRow = row as GridViewHierarchyRowInfo;
        if (hierarchyRow != null && hierarchyRow.IsCurrent == false)
        {
            hierarchyRow.IsExpanded = false;
        }
    }
}

Note, this is a sample demonstration. Feel free to modify it in a way to suit your requirements the best.

I hope this helps. If you need further assistance do not hesitate to contact me.

Regards,
Nadya
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/.

Asked by
James
Top achievements
Rank 1
Veteran
Answers by
Nadya
Telerik team
James
Top achievements
Rank 1
Veteran
Share this question
or