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

Self-referencing hierarchy Grid Programmatically

6 Answers 201 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Minas
Top achievements
Rank 1
Minas asked on 24 Nov 2008, 05:48 PM

Hello,

I've been trying the Rad Componets for ASP.net and a small issue came up.

I was trying to create a Self-referencing hierarchy grid programmatically on the Page_Init Event.

While I have been successful in creating the grid in the designer view, when I set up it's structure in the Page_Init the hierarchy doesn't seem to be working (ie I get all the records and columns to show normally but they don't have any children).

Is it possible to give me a few pointers on what I need to create the Self-referencing structure on the grid?

PS:My code looks like:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SelfHierarchyRadGrid._Default" %>  
 
<%@ Register assembly="Telerik.Web.UI, Version=2008.3.1105.35, Culture=neutral, PublicKeyToken=121fae78165ba3d4" namespace="Telerik.Web.UI" tagprefix="telerik" %>  
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 
<html xmlns="http://www.w3.org/1999/xhtml" >  
<head runat="server">  
    <title></title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <telerik:RadScriptManager runat="server">  
    </telerik:RadScriptManager>  
    <div>  
      
        <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" GridLines="None">  
        </telerik:RadGrid>  
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"   
            ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>"   
            SelectCommand="SELECT [aa], [parentaa], [Text] FROM [HierarchyTest]">  
        </asp:SqlDataSource>  
      
    </div>  
    </form>  
</body>  
</html>  
 
protected void Page_Init(object sender, EventArgs e)  
{  
  if (!IsPostBack)  
  {  
    InitGrid();  
  }  
}  
private void DefineStructure()  
{  
   RadGrid1.DataSourceID = "SqlDataSource1";  
 
   RadGrid1.MasterTableView.HierarchyDefaultExpanded = true;  
   RadGrid1.MasterTableView.HierarchyLoadMode = GridChildLoadMode.Client;  
   RadGrid1.MasterTableView.AllowSorting = true;  
   string[] dkn={"aa""parentaa"};  
   RadGrid1.MasterTableView.DataKeyNames = new string[] { "aa""parentaa" };  
   RadGrid1.MasterTableView.SelfHierarchySettings.ParentKeyName = "parentaa";  
   RadGrid1.MasterTableView.SelfHierarchySettings.KeyName = "aa";  
   RadGrid1.ClientSettings.AllowExpandCollapse = true;  
 
   RadGrid1.AutoGenerateColumns = false;  
   RadGrid1.ClientSettings.Selecting.AllowRowSelect = true;  
 
   //Create Columns  
   GridBoundColumn boundcolumn = new GridBoundColumn();  
   boundcolumn.datafield="aa";  
   RadGrid1.MasterTableView.Add(boundcolumn);  
   boundcolumn.UniqueName = "aa";  
   boundcolumn.Visible = true;  
   boundcolumn.HeaderText = "aa";  
 
   //... Rest of the columns ...  

6 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 27 Nov 2008, 07:34 AM
Hi Minas,

To see more information on the most important aspects of creating the hierarchy structure, please refer to the following example.
I hope it gets you started properly.

Regards,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Minas
Top achievements
Rank 1
answered on 27 Nov 2008, 11:40 AM

Hello,

I tried your example before and it works perfectly when I create on design time.

My problem is achieving the same result in code behind (ie runtime creation).

I appear to be missing or doing something wrong when trying to create the self-referencing hierarchy .

Creating columns dynamically works fine, but creating the self-refrencing hiearchy doesn't  seem to work.

Thank you for your time.

0
Yavor
Telerik team
answered on 27 Nov 2008, 12:35 PM
Hello Minas,

Based on the example supplied, are you calling the CreateExpandCollapseButton to add the expand collapse button which shows/hides the inner tables?

Kind regards,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Minas
Top achievements
Rank 1
answered on 27 Nov 2008, 05:39 PM

Thank you for your quick reply. I'm indeed calling the function but that doesn't seem to be the problem.

I did some testing and found that when I created an expand/collapse column on the code behind I was unable to click on the button (of the column). The button created from the function seems to be working children columns don't seem to appear.
C#:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using Telerik.Web.UI;  
 
namespace SelfHierarchyRadGrid  
{  
    public partial class _Default : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            RadGrid1.MasterTableView.FilterExpression = @"it[""parentaa""] = Convert.DBNull";  
        }  
 
        protected void Page_Init(object sender, EventArgs e)  
        {  
            if (!IsPostBack)  
            {  
                InitGrid();  
            }  
            //SqlDataSource1.DataBind();  
        }  
 
        public void Page_PreRenderComplete(object sender, EventArgs e)  
        {  
            HideExpandColumnRecursive(RadGrid1.MasterTableView);  
        }  
 
        private void InitGrid()  
        {  
            RadGrid1.DataSourceID = "SQLDataSource1";  
 
            RadGrid1.MasterTableView.HierarchyDefaultExpanded = true;  
            RadGrid1.MasterTableView.HierarchyLoadMode = GridChildLoadMode.Client;  
            RadGrid1.MasterTableView.AllowSorting = true;  
            string[] dkn={"aa""parentaa"};  
            RadGrid1.MasterTableView.DataKeyNames = new string[] { "aa""parentaa" };  
            RadGrid1.MasterTableView.SelfHierarchySettings.ParentKeyName = "parentaa";  
            RadGrid1.MasterTableView.SelfHierarchySettings.KeyName = "aa";  
            RadGrid1.ClientSettings.AllowExpandCollapse = true;  
 
            RadGrid1.AutoGenerateColumns = false;  
 
            //Create Columns  
            GridBoundColumn boundcolumn = new GridBoundColumn();  
            RadGrid1.MasterTableView.Columns.Add(boundcolumn);  
            boundcolumn.DataField = "aa";  
            boundcolumn.UniqueName = "aa";  
            boundcolumn.HeaderText = "aa";  
 
            GridBoundColumn boundcolumn2 = new GridBoundColumn();  
            RadGrid1.MasterTableView.Columns.Add(boundcolumn2);  
            boundcolumn2.DataField = "parentaa";  
            boundcolumn2.UniqueName = "parentaa";  
            boundcolumn2.HeaderText = "parentaa";  
 
            GridBoundColumn boundcolumn3 = new GridBoundColumn();  
            RadGrid1.MasterTableView.Columns.Add(boundcolumn3);  
            boundcolumn3.DataField = "Text";  
            boundcolumn3.UniqueName = "Text";  
            boundcolumn3.HeaderText = "Text";  
            //Telerik.Web.UI.GridExpandColumn expcol = new Telerik.Web.UI.GridExpandColumn();  
            //RadGrid1.MasterTableView.Columns.Add(expcol);  
        }  
 
        protected void RadGrid1_ColumnCreated(object sender, Telerik.Web.UI.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 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";  
        }  
 
        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.Items.Count == 0)  
                    {  
                        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, "aa");  
 
            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, "aa");  
        }  
    }  
}  
 


ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SelfHierarchyRadGrid._Default" %>  
 
<%@ Register assembly="Telerik.Web.UI, Version=2008.3.1105.35, Culture=neutral, PublicKeyToken=121fae78165ba3d4" namespace="Telerik.Web.UI" tagprefix="telerik" %>  
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 
<html xmlns="http://www.w3.org/1999/xhtml" >  
<head runat="server">  
    <title></title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <telerik:RadScriptManager runat="server">  
    </telerik:RadScriptManager>  
    <div>  
      
        <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1"   
            GridLines="None" oncolumncreated="RadGrid1_ColumnCreated"   
            onitemcreated="RadGrid1_ItemCreated" onitemdatabound="RadGrid1_ItemDataBound">  
        </telerik:RadGrid>  
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"   
            ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>"   
            SelectCommand="SELECT [aa], [parentaa], [Text] FROM [HierarchyTest]">  
        </asp:SqlDataSource>  
      
    </div>  
    </form>  
</body>  

</html>  

 

0
Yavor
Telerik team
answered on 01 Dec 2008, 10:40 AM
Hi Minas,

I reviewed the code snippet and it looks correct.
If the issue persists, however, you can open a formal support ticket, and send us a small project, demonstrating your setup, and the unwanted behavior, for additional review.

Kind regards,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Pavel
Top achievements
Rank 1
answered on 12 Dec 2011, 12:08 PM
Hello, I have same problem like Minas, could anybody answer, what was the problem?
Tags
Grid
Asked by
Minas
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Minas
Top achievements
Rank 1
Pavel
Top achievements
Rank 1
Share this question
or