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

Self-reference hierarchy using List<Object>

2 Answers 94 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David zhao
Top achievements
Rank 1
David zhao asked on 02 Oct 2011, 06:33 PM
Hi.
When I used List<object> type as datasource for self-reference hierarchy, I had a problem.
Here is my grid definition:
<telerik:RadGrid ID="RadGrid2" runat="server" DataSourceID="ObjectDataSource1"
            oncolumncreated="RadGrid2_ColumnCreated"
            onitemcreated="RadGrid2_ItemCreated"
            onitemdatabound="RadGrid2_ItemDataBound" CellSpacing="0" GridLines="None">
            <MasterTableView HierarchyLoadMode="Client" HierarchyDefaultExpanded="true"
                Width="100%"  AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" DataKeyNames="ID, ParentID">
                <SelfHierarchySettings ParentKeyName="ParentID" KeyName="ID" />
                <Columns>
                    <telerik:GridBoundColumn DataField="ID" DataType="System.Int32"
                        FilterControlAltText="Filter ID column" HeaderText="ID" SortExpression="ID"
                        UniqueName="ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="ParentID" DataType="System.Int32"
                        FilterControlAltText="Filter ParentID column" HeaderText="ParentID"
                        SortExpression="ParentID" UniqueName="ParentID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Name"
                        FilterControlAltText="Filter Name column"
                        HeaderText="Name" SortExpression="Name"
                        UniqueName="Name">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
            <ClientSettings AllowExpandCollapse="true" />
        </telerik:RadGrid>

    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid2">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid2" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
        SelectMethod="GetAllTest"
        TypeName="BLL.Test">       
    </asp:ObjectDataSource>
    <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
    </telerik:RadScriptManager>

===============
Here is my code

    protected void Page_Load(object sender, EventArgs e)
    {      
        RadGrid2.MasterTableView.FilterExpression = @"it[""ParentID""] == 0";      
    }

    public void Page_PreRenderComplete(object sender, EventArgs e)
    {
        HideExpandColumnRecursive(RadGrid2.MasterTableView);
    }

    protected void RadGrid2_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        CreateExpandCollapseButton(e.Item, "ID");
        if (e.Item is GridHeaderItem && e.Item.OwnerTableView != RadGrid2.MasterTableView)
        {
            e.Item.Style["display"] = "none";
        }
        if (e.Item is GridNestedViewItem)
        {
            e.Item.Cells[0].Visible = false;
        }
    }

    protected void RadGrid2_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 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 RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
    {
        CreateExpandCollapseButton(e.Item, "ID");
    }

    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 - 1;

                button.Style["margin-left"] = level * 15 + "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";
    }
==================
here is my BLL code

public class Test
    {
        private static readonly ITestDAL dal = DataAccess.CreateTestDAL();

        public static List<TestEntity> GetAllTest()
        {
            List<TestEntity> result = null;

            try
            {
                result = dal.GetAllTest();
            }
            catch (Exception ex)
            {
               .....
            }           

            return result;
        }
    }
==================
public class TestEntity
    {      
        private int id;
        private int parentID;
        private string name;

        public int ID
        {
            get { return this.id; }
            set { this.id = value; }
        }

        public int ParentID
        {
            get { return this.parentID; }
            set { this.parentID = value; }
        }

        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }

        public TestEntity()
        {
        }

        public TestEntity(int id, int parentID, string name)
        {
            this.id = id;
            this.parentID = parentID;
            this.name = name;
        }

    }

==================
here is error message:
ExceptionType:Telerik.Web.UI.ParseException
ErrorMessage:No applicable indexer exists in type 'TestEntity'
ErrorSource:Telerik.Web.UI

==================
Can you please find the reason for the error ? Or , can you tell me how to realize self-reference hierarchy with List<Object> as datasource

2 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 03 Oct 2011, 12:13 PM
Hi David,

In order to implement self-referencing grid with you custom objects and collection, you should disable grid linq expressions. Check the attached demo.

Kind regards,
Iana Tsolova
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
0
David zhao
Top achievements
Rank 1
answered on 03 Oct 2011, 05:53 PM
Iana Tsolova:

      Thank you very much!

      The demo you given works well.  I tried it and  found my error. In my codes, FilterExpression is wrong.
Tags
Grid
Asked by
David zhao
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
David zhao
Top achievements
Rank 1
Share this question
or