New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Traversing detail tables/items in Telerik RadGrid

Accessing the DetailTables through NestedTableViews Collection

If you have a hierarchical grid each item in GridTableView's Items collection has a child item of type GridNestedViewItem that has a set of DetailTables. So if you want to access, for example, the nested table view of the first item in the grid's master table you should have the following code:

C#
GridTableView nestedTableView = (RadGrid1.MasterTableView.Items[0] as GridDataItem).ChildItem.NestedTableViews[0];

Or if you have a reference to an instance of an item in a child table and if you want to access the parent item/parent table view you have to write the following code:

C#
GridDataItem parentItem = childItem.OwnerTableView.ParentItem as GridDataItem;

Looping through all detail tables/items in Telerik RadGrid

Before proceeding with the rest of this chapter we recommend you reading the KB article of Telerik RadGrid concerning Hierarchical binding tips. After reading the article, you will understand that each copy of a detail table corresponding to an item from the parent table resides in a NestedViewItem. You can iterate through the NestedViewItems in the grid using a recursive method, starting from the MasterTableView (note that the proper place to make the loop is the PreRender handler of the grid):

C#
void LoopHierarchyRecursive(GridTableView gridTableView)
{
    foreach (GridNestedViewItem nestedViewItem in gridTableView.GetItems(GridItemType.NestedView))
    {
        // you should skip the items if not expanded, or tables not bound
        if (nestedViewItem.NestedTableViews.Length > 0)
        {
            // now you can access: nestedViewItem.NestedTableViews[0].Items, which will be the DataItems of this nested table
            // then make recursive call
            LoopHierarchyRecursive(nestedViewItem.NestedTableViews[0]);
            // above [0] stands for the first table in the hierarchy, since Telerik RadGrid supports multiple tables at a level
        }
    }
}

When HieararchyLoadMode of the relevant GridTableView is "Client" or "ServerBind" a much easier approach could be used. The RadGrid.Items collection contains all items from all tables in the hierarchical structure of Telerik RadGrid. By simply looping through the collection you can access all data-bound items and their controls:

C#
foreach (GridDataItem item in RadGrid1.Items)
{
    if (item.OwnerTableView.Name == "MyTableName")
    {
        // if you need you may also check the parent item keys here
        if ((item.OwnerTableView.ParentItem as GridDataItem).GetDataKeyValue("MyKeyFieldName").ToString() == "some key value")
        {
            // operate with the controls in a custom manner
        }
    }
}

Looping through the detail tables/items in Telerik RadGrid on the client

You can also loop through the available GridTableView and GridDataItem client objects of a hierarchical RadGrid. Using the client API of the control, you can access the detail tables and items through a recursion similar to that inside the LoopHierarchyRecursive server-side method described above.

JavaScript
<script type="text/javascript">
    function pageLoad() {
        var grid = $find('<%=RadGrid1.ClientID %>');
        var masterTable = grid.get_masterTableView();
        traverseChildTables(masterTable);
    }

    function traverseChildTables(gridTableView) {
        var dataItems = gridTableView.get_dataItems();
        for (var i = 0; i < dataItems.length; i++) {
            if (dataItems[i].get_nestedViews().length > 0) {
                var nestedView = dataItems[i].get_nestedViews()[0];
                //here you can access the nested table's data items using nestedView.get_dataItems()
                alert(nestedView.get_name());
                traverseChildTables(nestedView);
            }
        }
    }
</script>