Allow only one row to expand in a hierarchical RadGridView

5 posts, 0 answers
  1. James
    James avatar
    7 posts
    Member since:
    Oct 2016

    Posted 31 Dec 2020 Link to this post

    How do I allow only one row in a hierarchical RadGridView to expand at a time?
  2. Nadya
    Admin
    Nadya avatar
    509 posts

    Posted 04 Jan Link to this post

    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/.

  3. James
    James avatar
    7 posts
    Member since:
    Oct 2016

    Posted 04 Jan in reply to Nadya Link to this post

    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,

  4. James
    James avatar
    7 posts
    Member since:
    Oct 2016

    Posted 05 Jan Link to this post

    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,

  5. Nadya
    Admin
    Nadya avatar
    509 posts

    Posted 06 Jan Link to this post

    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/.

Back to Top