Home / Community & Support / Knowledge Base / RadControls for WinForms / GridView / Hide expand/collapse image in hierarchical RadGridView

Hide expand/collapse image in hierarchical RadGridView

Article Info

Rating: 4

Article information

Article relates to

 RadGridView for WinForms, Q2 2010

Created by

 Nikolay Diyanov

Last modified

 July 27, 2010

Last modified by

 Alexander Georgiev, Telerik



INTRODUCTION

When RadGridView displays hierarchical data, you expand/collapse child levels in the hierarchy with the help of GridGroupExpanderCellElement containing an expand/collapse image. However, when you do not have child data for certain rows, you may not want to show the expand/collapse images in the GridGroupExpanderCellElement.

SOLUTION

First of all, we will create a method which returns true if there is child data and false if there is no child data for a parent row:
private bool IsExpandable(GridViewRowInfo rowInfo)
{
    if (rowInfo.ChildRows != null && rowInfo.ChildRows.Count > 0)
    {
        return true;
    }
 
    return false;
}

Next, we will subscribe to the ViewCellFormatting and ChildViewExpanding events of RadGridView. In ViewCellFormatting we will hide the expand/collapse image while in the ChildViewExpanding we will prevent a row from being expanded if there is no child data:
private void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
{
    GridGroupExpanderCellElement cell = e.CellElement as GridGroupExpanderCellElement;
    if (cell != null && e.CellElement.RowElement is GridDataRowElement)
    {
        if (!IsExpandable(cell.RowInfo))
        {
            cell.Expander.Visibility = Telerik.WinControls.ElementVisibility.Hidden;
        }
        else
        {
            cell.Expander.Visibility = Telerik.WinControls.ElementVisibility.Visible;
        }
    }
}
  
private void radGridView1_ChildViewExpanding(object sender, ChildViewExpandingEventArgs e) 
    e.Cancel = !IsExpandable(e.ParentRow); 
}

Comments

If you'd like to comment on this KB article, please, send us a Support Ticket.
Thank you!

Please Sign In to rate this article.