Hiding expand/collapse columns with Telerik RadGrid

Often times when using nested grids, you may desire to hide the expand/collapse button if there are no child items. This is very easy to do, although it’s not as intuitive as you might think. This example will show you how to accomplish this task using a Telerik RadGrid for ASP.Net Ajax.

  1. First, subscribe to the PreRender event of the Grid control.
  2. Loop through the GridNestedViewItems.
  3. Apply your visibility logic.
    protected void rgParts_PreRender(object sender, EventArgs e)
    {
        GridItem[] nestedViewItems = rgParts.MasterTableView.GetItems(GridItemType.NestedView);
        foreach (GridNestedViewItem nestedViewItem in nestedViewItems)
        {
            foreach (GridTableView nestedView in nestedViewItem.NestedTableViews)
            {
                if (((Foo)nestedView.ParentItem.DataItem).Prop1 == "DontShowExpandColmn")
                {
                    TableCell cell = nestedView.ParentItem["ExpandColumn"];
                    cell.Controls[0].Visible = false;
                    nestedViewItem.Visible = false;
                }
            }
        }
    }
 

Hope this helps someone.

Happy coding,

Tom Hundley
Elegant Software Solutions

What are your thoughts?

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.