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

SelfHierarchySettings - DetailTableDataBind Problem

3 Answers 170 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ahmet Hoso
Top achievements
Rank 1
Ahmet Hoso asked on 07 Dec 2008, 01:01 AM
Hi All,

I try to make unlimited hierarchical radgrid with SelfHierarchySettings. But when i try to expand a node i can not see the child level inside "No child records to display."  I don't want to use  HierarchyLoadMode ="Client"  so can any body check and inform me what's the problem with my code

Regards ..

<body> 
    <form id="form1" runat="server">  
    <asp:ScriptManager ID="sm" runat="server" /> 
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnColumnCreated="RadGrid1_ColumnCreated" 
        OnItemCreated="RadGrid1_ItemCreated" OnItemDataBound="RadGrid1_ItemDataBound" 
        OnDetailTableDataBind="RadGrid1_DetailTableDataBind">  
        <MasterTableView AllowSorting="true" DataKeyNames="FullPath,ParentPath,IsFolder" 
            Width="100%" > 
            <SelfHierarchySettings ParentKeyName="FullPath" KeyName="ParentPath" /> 
        </MasterTableView> 
        <ClientSettings AllowExpandCollapse="true" /> 
    </telerik:RadGrid> 
    </form> 
</body> 

 

public partial class test2 : System.Web.UI.Page  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        if (!Page.IsPostBack)  
        {  
            GridBoundColumn col = new GridBoundColumn();  
            col.HeaderText = "FullPath";  
            col.DataField = "FullPath";  
            RadGrid1.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.HeaderText = "ParentPath";  
            col.DataField = "ParentPath";  
            RadGrid1.Columns.Add(col);  
            RadGrid1.DataSource = GetData("/");  
 
        }  
    }  
 
    private DataTable GetData(string ParentPath)  
    {  
        DataTable dt = new DataTable();  
        dt.Columns.Add(new DataColumn("FullPath"));  
        dt.Columns.Add(new DataColumn("ParentPath"));  
        dt.Columns.Add(new DataColumn("IsFolder"typeof(bool)));  
        DataRow dr;  
        for (int i = 0; i < 3; i++)  
        {  
            dr = dt.NewRow();  
            dr[0] = ParentPath + "Folder" + i;  
            dr[1] = ParentPath;  
            dr[2] = true;  
            dt.Rows.Add(dr);  
        }  
 
        for (int i = 0; i < 2; i++)  
        {  
            dr = dt.NewRow();  
            dr[0] = ParentPath + "File" + i.ToString() + ".txt";  
            dr[1] = ParentPath;  
            dr[2] = false;  
            dt.Rows.Add(dr);  
        }  
 
        return dt;  
    }  
 
    protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)  
    {  
        if (e.Column is GridExpandColumn)  
        {  
            e.Column.Visible = false;  
        }  
        else if (e.Column is GridBoundColumn)  
        {  
            e.Column.HeaderStyle.Width = Unit.Pixel(100);  
        }  
    }  
 
    public void Page_PreRenderComplete(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)  
            {  
                nestedView.Style["border"] = "0";  
 
                Button MyExpandCollapseButton = (Button)nestedView.ParentItem.FindControl("MyExpandCollapseButton");  
                if (nestedView.ParentItem.GetDataKeyValue("IsFolder").ToString() == "False")  
                {  
                    if (MyExpandCollapseButton != null)  
                    {  
                        MyExpandCollapseButton.Style["visibility"] = "hidden";  
                    }  
                    nestedViewItem.Visible = false;  
                }  
                else 
                {  
                    if (MyExpandCollapseButton != null)  
                    {  
                        MyExpandCollapseButton.Style.Remove("visibility");  
                    }  
                }  
 
                if (nestedView.HasDetailTables)  
                {  
                    HideExpandColumnRecursive(nestedView);  
                }  
            }  
        }  
    }  
 
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)  
    {  
        CreateExpandCollapseButton(e.Item, "FullPath");  
 
        if (e.Item is GridHeaderItem && e.Item.OwnerTableView != RadGrid1.MasterTableView)  
        {  
            e.Item.Style["display"] = "none";  
        }  
 
        if (e.Item is GridNestedViewItem)  
        {  
            e.Item.Cells[0].Visible = false;  
        }  
    }  
 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        CreateExpandCollapseButton(e.Item, "FullPath");  
    }  
 
    public void CreateExpandCollapseButton(GridItem item, string columnUniqueName)  
    {  
        if (item is GridDataItem)  
        {  
            if (item.FindControl("MyExpandCollapseButton") == null)  
            {  
                Button button = new Button();  
                button.Click += new EventHandler(button_Click);  
                button.CommandName = "ExpandCollapse";  
                button.CssClass = (item.Expanded) ? "rgCollapse" : "rgExpand";  
                button.ID = "MyExpandCollapseButton";  
 
                if (item.OwnerTableView.HierarchyLoadMode == GridChildLoadMode.Client)  
                {  
                    string script = String.Format(@"$find(""{0}"")._toggleExpand(this, event); return false;", item.Parent.Parent.ClientID);  
 
                    button.OnClientClick = script;  
                }  
 
                int level = item.ItemIndexHierarchical.Split(':').Length;  
 
                if (level > 1)  
                    button.Style["margin-left"] = level + 10 + "px";  
 
 
                TableCell cell = ((GridDataItem)item)[columnUniqueName];  
                cell.Controls.Add(button);  
                cell.Controls.Add(new LiteralControl("&nbsp;"));  
                cell.Controls.Add(new LiteralControl(((GridDataItem)item).GetDataKeyValue(columnUniqueName).ToString()));  
            }  
        }  
    }  
 
    void button_Click(object sender, EventArgs e)  
    {  
        ((Button)sender).CssClass = (((Button)sender).CssClass == "rgExpand") ? "rgCollapse" : "rgExpand";  
    }  
 
    protected void RadGrid1_DetailTableDataBind(Object source, GridDetailTableDataBindEventArgs e)  
    {  
        GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;  
        if (dataItem.GetDataKeyValue("IsFolder").ToString() == "True")  
        {  
            e.DetailTableView.DataSource = GetData(dataItem.GetDataKeyValue("FullPath").ToString());  
        }  
    }  
}  
 

3 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 09 Dec 2008, 11:59 AM
Hi Ahmet,

To see more information on the requested setup, please refer to the following example.
It shows how to set the datasource for the grid. Also, the detailTableDataBind handler is only meaningful when using a standard hierarchy, such as the one shown here. I hope this information helps.

All the best,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Richa
Top achievements
Rank 1
answered on 12 Apr 2014, 10:41 AM
Hi,

I have A Problem Like this.... In My Page I have Around 10,000 record in parent grid and it contains a detailtable grid and Each detail table grid Contains around 500 record. i am using HierarchyLoadMode="Client".  i Change HierarchyLoadMode to server on demand For binding Detail table after expand the row. but my page post back. and it again takes too much time. please suggest me the right way to do it. It's urgent. Please Reply soon.


Thanks

Richa





0
Pavlina
Telerik team
answered on 16 Apr 2014, 07:32 PM
Hello Richa,

The biggest performance decrease in cases like the illustrated is caused by the big amount of HTML that needs to be rendered. In order to improve the performance I suggest you setting a smaller page size and Conditional HierarchyLoadMode which will allow you to expand items on the client after they are initially expanded. Finally you could revise this help article for more tips and tricks on how to optimize the grid performance.

Regards,
Pavlina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Ahmet Hoso
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Richa
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or