New to Telerik UI for WinForms? Download free 30-day trial

Hide expand/collapse image in hierarchical RadGridView

Product Version Product Author Last modified
Q2 2010 SP1 RadGridView for WinForms Nikolay Diyanov July 27, 2010

PROBLEM

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); 
}
In this article