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

[Solved] Expand row if child table has rows

5 Answers 616 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Venu Daroju
Top achievements
Rank 1
Venu Daroju asked on 28 Jan 2010, 11:16 PM
Hi i was using

http://demos.telerik.com/aspnet-ajax/grid/examples/programming/detailtabledatabind/defaultcs.aspx

approach to display a grid with one child table, and data is all driven from code behind.

in the example C# code we have following code to expand first row

            protected void RadGrid1_PreRender(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
RadGrid1.MasterTableView.Items[0].Expanded = true;
RadGrid1.MasterTableView.Items[0].ChildItem.NestedTableViews[0].Items[0].Expanded = true;
}
}

Question is how do i expand rows who's child table has more than zero record in it and other rows which doesn't have any
records should not be expanded.

Please let me know... 

Thank you
Venu.

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 29 Jan 2010, 05:15 AM
Hi,

Try the code snippet below to achieve the desired scenario. Here are the steps to accomplish this:

1)Wire the PreRender event of Telerik RadGrid
2)Traverse all grid items and get reference to those which are of type GridNestedViewItem
3)Verify whether their NestedTableViews collection is empty
4)If empty then prevent the Parent item from expanding

C#

protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        HideExpandColumnRecursive(RadGrid1.MasterTableView); 
 
    } 
public void HideExpandColumnRecursive(GridTableView tableView) 
    { 
       GridItem[] nestedViewItems = tableView.GetItems(GridItemType.NestedView); 
       foreach (GridNestedViewItem nestedViewItem in nestedViewItems) 
        { 
            foreach (GridTableView nestedView in nestedViewItem.NestedTableViews) 
            { 
                if (nestedView.Items.Count == 0) 
                { 
                    nestedView.ParentItem.Expanded= false
                } 
 
                else 
                { 
                    nestedView.ParentItem.Expanded = true
                } 
            } 
        } 
    }  
 

Thaks,
Princy

0
Venu Daroju
Top achievements
Rank 1
answered on 29 Jan 2010, 06:18 AM
Thanks Princy,

But it didn't work, it colapses all rows

FYI: i just have only one table inside master table.

Thank you
Venu.
0
Schlurk
Top achievements
Rank 2
answered on 01 Feb 2010, 04:26 PM
If you are using DetailTables the following should work:

            foreach (GridTableView myView in RadGrid1.MasterTableView.DetailTables) 
            { 
                if (myView.Items.Count == 0) 
                { 
                    myView.ParentItem.Expanded = false
                } 
                else 
                { 
                    myView.ParentItem.Expanded = true
                } 
            } 

It's not the most efficient method but it works :) Also I think Princy's suggestion doesn't work because you seem to be using DetailTables while his approach uses NestedViews.
0
Venu Daroju
Top achievements
Rank 1
answered on 02 Feb 2010, 03:24 PM
Hello Schlurk,

thank you for your solution, but i get following error

 

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 108:                    {
Line 109:
Line 110: myView.ParentItem.Expanded = false;Line 111:                    }
Line 112:                    else


if i comment the true part of if statement it works fine with out any error,  but it colapses all rows in the grid, which we do not want.

Please let me know thank you

Thank you
Venu.
0
Radoslav
Telerik team
answered on 05 Feb 2010, 01:41 PM
Hi Venu,

Could you please try to set HierarchyLoadMode property to ServerBind and see if it makes any difference:

<MasterTableView DataSourceID="SqlDataSource1" HierarchyLoadMode="ServerBind" DataKeyNames="DataKeyNames" AllowMultiColumnSorting="True">


Then you could achieve the desired functionality with the code which Princy posted:

protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        HideExpandColumnRecursive(RadGrid1.MasterTableView);
  
    }
public void HideExpandColumnRecursive(GridTableView tableView)
    {
       GridItem[] nestedViewItems = tableView.GetItems(GridItemType.NestedView);
       foreach (GridNestedViewItem nestedViewItem in nestedViewItems)
        {
            foreach (GridTableView nestedView in nestedViewItem.NestedTableViews)
            {
                if (nestedView.Items.Count == 0)
                {
                    nestedView.ParentItem.Expanded= false;
                }
  
                else
                {
                    nestedView.ParentItem.Expanded = true;
                }
            }
        }
    

The default value of the HierarchyLoadMode property is ServerOnDemand, however this property tells to the RadGrid to render only items which are expanded. If you try to count the child items of every parent item, the count will be always 0. So initially in the RadGrid.PreRender event do not exist any children elements and the RadGrid renders collapsed parent items.

Also you could check out this online documentation article which contains information about HierarchyLoadMode property:

http://www.telerik.com/help/aspnet-ajax/telerik.web.ui-telerik.web.ui.gridtableview-hierarchyloadmode.html

Additionally I am sending you a simple example that illustrate the how to achieve the desired functionality.

All the best,
Radoslav
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
Tags
Grid
Asked by
Venu Daroju
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Venu Daroju
Top achievements
Rank 1
Schlurk
Top achievements
Rank 2
Radoslav
Telerik team
Share this question
or