This is a migrated thread and some comments may be shown as answers.

Self Referencing hierarchy grid : Expand All

1 Answer 66 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bill
Top achievements
Rank 1
Bill asked on 28 Mar 2012, 11:19 PM
Hi,

I have a self-referencing grid.  I'm declaring it and configuring it all in the aspx declaration and binding it via the "rebind"-"needs-data" approach.   It works fantastic.

The grid is initialized with a large data set (using the HierarchyLoadMode="ServerOnDemand") mode, and I have some PreRender code that expands the first 2 levels of the hierarchy.

Now the user can filter data, we are not using the grids filter but re-querying the database and returning a new dataset and rebinding to the grid.  If that dataset contains less than 20 records, the user wants all the nodes expanded by default, otherwise only the first 2.

I've tried several approaches: 
1.) Expanding in pre-render.  Doesn't work, only 2 sub-layers have data.
2.) changing HierarchyLoadMode to "ServerBind" and expanding in Pre-Render using a recursive expand function (code listed below)   Seems to work until the 4th sub-level, after the 4th sublevel the item.ChildItem.NestedTableViews.count is 0, even though I know there are 6 levels in the data set.

My next step is to try it with HierarchyLoadMode = "client" and my last ditch effort to dynmically build the grid for every request.  any guidence would be appreciated.



protected void RadGrid1_PreRender(object sender, EventArgs e)
        {

            if (ExpandAll)
            {
                RadGridExpandHierarchy(RadGrid1.Items);

            }
}

        private void RadGridExpandHierarchy(GridDataItemCollection items)
        {
            foreach (GridDataItem item in items)
            {
                if (item.GetDataKeyValue("IsPolicyRow").ToString()=="0" && item.ChildItem != null && item.ChildItem.NestedTableViews != null && item.ChildItem.NestedTableViews.Count() > 0)
                {
                    GridTableView childtableView = (GridTableView)item.ChildItem.NestedTableViews[0];
                    RadGridExpandHierarchy(childtableView.Items);
                }
                if (item.CanExpand && item.GetDataKeyValue("IsPolicyRow").ToString() == "0")
                    item.Expanded = true;

            }

        }

1 Answer, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 02 Apr 2012, 03:12 PM
Hi Bill,

I would advise you to use the RadTreeList control which is designed for displaying self-referencing data. With it, the code for expanding the items as required would look similar to this:
protected void RadTreeList1_PreRender(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (RadTreeList1.Items.Count < 2)
        {
            RadTreeList1.ExpandAllItems();
        }
        else
        {
            RadTreeList1.Items[0].Expanded = true;
            RadTreeList1.Items[1].Expanded = true;
            RadTreeList1.Rebind();
        }
    }
}


All the best,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Bill
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Share this question
or