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

Self Hierarchy and DetailTables

8 Answers 307 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 27 Oct 2009, 09:51 PM
I have a Grid setup with a self hierarchy with additional detail tables.  When I expand a top level entry, I correctly see the children (both the linked detailtables and also the self hierarchy entries).  But when I expand the next level of a entry from the self hierarchy, the linked detailtables are not displayed.

I am using the NeedDataSource method on the Grid, which checks if the event is from the detail table or not.
And also using the DetailTableDataBind as described at http://demos.telerik.com/aspnet-ajax/grid/examples/programming/detailtabledatabind/defaultcs.aspx, and when I step through the code in the debugger, the DetailTableDataBind never gets called for the detail tables when I expand below the first level.

An example of what I am trying to do is a Folder table and a File table.  You can have Folders within Folders, and Files in Folders.  If I expand the top level Folder, I can correctly see the child folders and files, but when I expand one of the child folders, it only shows folders.  It seems to skip over the detailtables.

Is this a bug, or might I be overlooking something?

Thanks,
Eric

8 Answers, 1 is accepted

Sort by
0
Eric
Top achievements
Rank 1
answered on 28 Oct 2009, 05:05 AM
Example Code:
   <telerik:RadScriptManager ID="RadScriptManager1" runat="server" /> 
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"
        <AjaxSettings> 
            <telerik:AjaxSetting AjaxControlID="RadGrid1"
                <UpdatedControls> 
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" /> 
                </UpdatedControls> 
            </telerik:AjaxSetting> 
        </AjaxSettings> 
    </telerik:RadAjaxManager> 
    <telerik:RadGrid ID="RadGrid1" AutoGenerateColumns="true" ShowHeader="true" runat="server" 
        Width="97%" GridLines="None" OnNeedDataSource="RadGrid1_NeedDataSource" OnDetailTableDataBind="RadGrid1_DetailTableDataBind"
        <MasterTableView Name="Folders" DataKeyNames="Id, folder_id" GridLines="Both" PageSize="15" 
            HierarchyDefaultExpanded="false" HierarchyLoadMode="ServerOnDemand"
            <SelfHierarchySettings KeyName="Id" ParentKeyName="folder_id" /> 
            <DetailTables> 
                <telerik:GridTableView runat="server" DataKeyNames="Id" Name="Files_Folder" HierarchyLoadMode="ServerOnDemand" 
                    AllowPaging="False"
                </telerik:GridTableView> 
            </DetailTables> 
        </MasterTableView> 
    </telerik:RadGrid> 

        public void Page_Load(object sender, EventArgs e) 
        { 
            if (!IsPostBack) 
            { 
                RadGrid1.MasterTableView.FilterExpression = "Folder_Id IS NULL"
            } 
        } 
 
        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
        { 
            if (!e.IsFromDetailTable) 
            { 
                string sql = "SELECT Id, Name, Folder_Id FROM Folder;"
 
                RadGrid1.DataSource = GetDataTable(sql); 
            } 
        } 
 
        protected void RadGrid1_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e) 
        { 
            GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem; 
            switch (e.DetailTableView.Name) 
            { 
                case "Folders"
                    { 
                        string parentId = dataItem.GetDataKeyValue("Id").ToString(); 
                        e.DetailTableView.DataSource = GetDataTable("SELECT Name, Id, Folder_Id FROM Folder WHERE Folder_Id = " + parentId); 
                        break
                    } 
 
                case "Files_Folder"
                    { 
                        string parentId = dataItem.GetDataKeyValue("Id").ToString(); 
                        string sql = "SELECT Id, Name, Folder_id FROM AttachedFile WHERE Folder_id = " + parentId; 
                        e.DetailTableView.DataSource = GetDataTable(sql); 
                        break
                    } 
            } 
        } 
 
        public DataTable GetDataTable(string query) 
        { 
            String ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ArchiveConnectionString"].ToString(); 
            SqlConnection conn = new SqlConnection(ConnString); 
            SqlDataAdapter adapter = new SqlDataAdapter(); 
            adapter.SelectCommand = new SqlCommand(query, conn); 
 
            DataTable myDataTable = new DataTable(); 
 
            conn.Open(); 
            try 
            { 
                adapter.Fill(myDataTable); 
            } 
            finally 
            { 
                conn.Close(); 
            } 
 
            return myDataTable; 
        } 
 

0
Veli
Telerik team
answered on 30 Oct 2009, 10:19 AM
Hello Eric,

RadGrid's self-referencing hierarchy functionality has been designed to work with a single flat data table that you assign to RadGrid's DataSouce. I see that you are redefining your detail table data source, which is not a supported scenario in RadGrid.

If you share with us what you are trying to achieve, we might be able to suggest a more appropriate approach.

Regards,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Eric
Top achievements
Rank 1
answered on 02 Nov 2009, 03:10 PM
Telerik Team,

Think of a a folder hierarchy where the folders can contain other folders and/or files, similar to the look provided by a Windows Explorer window, but within a single grid control.  The Folder table would be self referencing to traverse the tree of folders, while also allowing you to see the files within each folder when expanded.  Something similar to the example below.

- Folder A 
     File 1 
     File 2 
     - Folder A.1 
           File 3 
           + Folder A.1.1 
+ Folder B 


Does that make sense?  Is there an alternate approach to solve this?  If not, I may need to look to other controls.

Thanks,
Eric

0
Veli
Telerik team
answered on 04 Nov 2009, 03:25 PM
Hi Eric,

You can find attached a small web page I have created to demonstrate a similar implementation to a folders tree scenario. You can note that I only have a single data source, which is formed by recursively looping over all subdirectories in a directory and adding directory data to the collection.

Greetings,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Anoop
Top achievements
Rank 1
answered on 02 Jul 2010, 11:20 AM
HI,

I have the same issue regarding hierarchy grid . Let me explain in brief

I have three tables 
First one is Domain Table  Carrying Domain Id and Domain Name( Like Yahoo, google)
Second Table is Sender Info Table which contains SenderId,EmailAddress, FileID
Third Table is File table which contain file info.  File Id, FileName , Parent FileID,

Now I want to make a structure like 

DomainName(Yahoo)
Email Address(anoop@yahoo.com)
FileName 1 
FileName1.1
FileName1.1.1
FileName1.1.2
FileName1.2
FileName 2
FileName2.1
FileName2.2

DomainName(Google.com)
Email Address(anoop@gmail.com)
FileName 1 
FileName1.1
FileName1.1.1
FileName1.1.2
FileName1.2
FileName 2

FileName2.1
FileName2.2

As the file table contains self referential relation  I want that upto n level .
My main concern is self referential grid  as i am not able to keep self referential rad Grid    inside DetailTables in Main RadGrid
Or is there any other alternative . Please suggest



0
Veli
Telerik team
answered on 07 Jul 2010, 02:35 PM
Hello Anoop,

As far as I understand, your first, second and third level hierarchy data is all different. Then from third level on, you have the same fields in all levels. This means you can have a regular hierarchy of detail tables in the first and second level, while the third level can be a second nested RadGrid defining a self-referencing hierarchy structure:

<telerik:RadGrid ID="RadGrid1" runat="server">
    <MasterTableView Name="Level1">
        <DetailTables>
            <telerik:GridTableView Name="Level2">
                <NestedViewTemplate>
                    <telerik:RadGrid ID="RadGrid2" runat="server">
                        <MasterTableView Name="SelfHierarchyLevel1">
                            <SelfHierarchySettings KeyName="" ParentKeyName="" />
                        </MasterTableView>
                    </telerik:RadGrid>
                </NestedViewTemplate>
            </telerik:GridTableView>
        </DetailTables>
    </MasterTableView>
</telerik:RadGrid>

In the above sample markup, RadGrid1 has 2 levels of hierarchical detail tables - the MasterTableView and a nested DetailTableView. The nested detail table has a NestedViewTemplate that contains a second RadGrid (RadGrid2). RadGrid2 now has a self-referencing hierarchy setup where your actual files are enumerated in a hierarchical tree.

All the best,
Veli
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Anoop
Top achievements
Rank 1
answered on 16 Jul 2010, 06:25 AM
HI Veli,

Thanks for your previous reply. Now I am able to bind the structure as i needed.  See the code below:

************************************************************************************************************************************************************************

<telerik:RadGrid Skin="Web20" ID="grdDomain" runat="server" AllowPaging="True" AllowSorting="True"
    AutoGenerateColumns="False" EnableEmbeddedSkins="false" Width="100%" OnNeedDataSource="grdDomain_NeedDataSource"
    OnDetailTableDataBind="grdDomain_DetailTableDataBind" PageSize="10" GridLines="None">
    <MasterTableView ShowHeadersWhenNoRecords="false" AllowMultiColumnSorting="True"
        AutoGenerateColumns="false" HierarchyLoadMode="ServerOnDemand" runat="server"
        DataKeyNames="DomainID" ExpandCollapseColumn-Display="true" ExpandCollapseColumn-ItemStyle-Width="20px"
        ExpandCollapseColumn-FilterControlWidth="20px" ExpandCollapseColumn-HeaderStyle-Width="20px">
        <DetailTables>
             <telerik:GridTableView ShowHeader="false" Name="EmailSenderInfo" Width="100%" DataKeyNames="EmailAddress">
                <ParentTableRelation>
                    <telerik:GridRelationFields DetailKeyField="DomainID" MasterKeyField="DomainID" />
                </ParentTableRelation>
                <Columns>
                    <telerik:GridBoundColumn ItemStyle-Width="100%" HeaderStyle-Width="100%" DataField="EmailAddress"
                        HeaderText="EmailAddress" SortExpression="EmailAddress">
                    </telerik:GridBoundColumn>
                </Columns>
                <NestedViewTemplate>
                    <telerik:RadGrid AutoGenerateColumns="false" GridLines="None" GroupingEnabled="false"
                        Skin="Web20" AllowMultiRowSelection="true" EnableEmbeddedBaseStylesheet="false"
                        EnableEmbeddedSkins="false" ID="grdsearchresult" runat="server" OnItemDataBound="grdsearchresult_ItemDataBound"
                        OnPreRender="grdsearchresult_PreRender" OnSortCommand="grdsearchresult_SortCommand"
                        ShowHeader="true" OnItemCreated="grdsearchresult_ItemCreated" OnNeedDataSource="grdsearchresult_NeedDataSource">
                        <MasterTableView ShowHeadersWhenNoRecords="false" AllowSorting="true" HierarchyLoadMode="ServerOnDemand"
                            EnableNoRecordsTemplate="true" DataKeyNames="FileID,FileParentID" NoMasterRecordsText="No Master Records"
                            NoDetailRecordsText="No Detail Records" Width="100%" Height="100%">
                            <SelfHierarchySettings ParentKeyName="FileParentID" KeyName="FileID" />
                            <Columns>
                                <telerik:GridTemplateColumn UniqueName="Checkbox" HeaderStyle-Width="30px">
                                    <HeaderTemplate>
                                        <asp:CheckBox runat="server" ID="chkHeader" />
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:CheckBox runat="server" ID="chkSelectRow" />
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn Visible="false" Resizable="false" Display="false" UniqueName="ChildCount"
                                    HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" HeaderText="ChildCount">
                                    <ItemTemplate>
                                        <asp:Label ID="lblChildCount" runat="server" Text='<%# Eval("ChildCount")%>'></asp:Label>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn ItemStyle-Wrap="false" SortExpression="FileNameDisplay"
                                    UniqueName="FileName" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"
                                    HeaderText="File Name">
                                    <ItemTemplate>
                                        <asp:Label ID="lblFileName" ToolTip='<%#Eval("Filename")%>' runat="server" Text='<%# Eval("Filename")%>'></asp:Label>
                                    </ItemTemplate>
                                    <ItemStyle Wrap="false" />
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn ItemStyle-Wrap="true" SortExpression="Size" UniqueName="Size"
                                    HeaderStyle-HorizontalAlign="Left" HeaderText="Size (Bytes)" ItemStyle-HorizontalAlign="Right"
                                    HeaderStyle-Width="80px">
                                    <ItemTemplate>
                                        <asp:Label ID="lblSize" runat="server" Text='<%#FormatSize(Eval("Size"))%>'></asp:Label>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn ItemStyle-Wrap="false" SortExpression="FileTypeName"
                                    UniqueName="FileTypeName" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"
                                    HeaderText="File Type Name">
                                    <ItemTemplate>
                                        <asp:Label ID="lblFileTypeName" runat="server" Text='<%#Eval("FileTypeName")%>'></asp:Label>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn ItemStyle-Wrap="false" SortExpression="Custodian" Display="false"
                                    UniqueName="Custodian" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"
                                    HeaderText="Custodian">
                                    <ItemTemplate>
                                        <asp:Label ID="lblCustodian" runat="server" Text='<%#Eval("Custodian")%>'></asp:Label>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn ItemStyle-Wrap="false" UniqueName="TagName" HeaderStyle-HorizontalAlign="Left"
                                    ItemStyle-HorizontalAlign="Left" HeaderText="Tag Name">
                                    <ItemTemplate>
                                        <asp:Label ID="lblTagName" runat="server" Text='<%#Eval("TagName")%>'></asp:Label>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn ItemStyle-Wrap="false" UniqueName="ExportSetName" HeaderStyle-HorizontalAlign="Left"
                                    ItemStyle-HorizontalAlign="Left" HeaderText="Export Set Name">
                                    <ItemTemplate>
                                        <asp:Label ID="lblProductionSetName" runat="server" Text='<%#Eval("ProductionSetName")%>'></asp:Label>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn ItemStyle-Wrap="false" SortExpression="CreatedDate" UniqueName="CreatedDate"
                                    HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Right" HeaderText="Created Date"
                                    HeaderStyle-Width="90px">
                                    <ItemTemplate>
                                        <asp:Label ID="lblCreatedDateTime" runat="server" Text='<%#FormatDateTime(Eval("CreatedDate"))%>'></asp:Label>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn ItemStyle-Wrap="false" SortExpression="ModifiedDate"
                                    UniqueName="ModifiedDate" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Right"
                                    HeaderText="Modified Date" HeaderStyle-Width="90px">
                                    <ItemTemplate>
                                        <asp:Label ID="lblModifiedDateTime" runat="server" Text='<%#FormatDateTime(Eval("ModifiedDate"))%>'></asp:Label>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn ItemStyle-Wrap="false" UniqueName="HitCount" HeaderStyle-HorizontalAlign="Left"
                                    HeaderText="Hit Count" SortExpression="HitCount" ItemStyle-HorizontalAlign="Right"
                                    HeaderStyle-Width="80px">
                                    <ItemTemplate>
                                        <asp:Label ID="lblHitCount" runat="server" Text='<%#Eval("HitCount")%>'></asp:Label>
                                        <div style="display: none">
                                            <asp:Label ID="lblFileParentID" runat="server" Text='<%#Eval("FileID")%>'></asp:Label>
                                            <asp:Label ID="lblProjectID" runat="server" Text='<%#Eval("ProjectID")%>'></asp:Label>
                                            <asp:Label ID="lblEmailAddress" runat="server" Text='<%#Eval("EmailAddress")%>'></asp:Label>
                                        </div>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn ItemStyle-Wrap="false" Display="false" UniqueName="OurName"
                                    HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" HeaderText="Our Name">
                                    <ItemTemplate>
                                        <asp:Label ID="lblOurName" runat="server" Text='<%#Eval("DisplayName")%>'></asp:Label>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn ItemStyle-Wrap="false" Display="false" UniqueName="BatchName"
                                    HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" HeaderText="Batch Name">
                                    <ItemTemplate>
                                        <asp:Label ID="lblBatchName" runat="server" Text='<%#GetBatchName(Eval("BatchID"),Eval("ProjectID"))%>'></asp:Label>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn ItemStyle-Wrap="false" HeaderStyle-HorizontalAlign="Center"
                                    ItemStyle-HorizontalAlign="Center" UniqueName="View" HeaderStyle-Width="100px">
                                    <HeaderTemplate>
                                        View
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <div style="margin-left: 30px;">
                                            <div id="dvViewHtml" runat="server">
                                            </div>
                                            <div id="dvViewNative" runat="server" title="View file data in native mode" style="float: left;
                                                padding-left: 6px; cursor: pointer;">
                                            </div>
                                        </div>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                            </Columns>
                        </MasterTableView>
                        <ClientSettings Resizing-AllowColumnResize="true" AllowExpandCollapse="true" Scrolling-AllowScroll="false"
                            Selecting-AllowRowSelect="true">
                            <ClientEvents OnRowClick="OnRowClick" />
                            <ClientEvents OnColumnContextMenu="ColumnContextMenu" />
                        </ClientSettings>
                    </telerik:RadGrid>
                </NestedViewTemplate>
                <NoRecordsTemplate>
                    No Record(s) found...</NoRecordsTemplate>
            </telerik:GridTableView>
        </DetailTables>
        <Columns>
            <telerik:GridBoundColumn DataField="DomainName" HeaderText="Domain Name" SortExpression="DomainName">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
    <ClientSettings AllowExpandCollapse="True">
    </ClientSettings>
</telerik:RadGrid>
********************************************************************************************************************************************************************

Now my problem is regading the binding of radgrid inside nested View Template (  <NestedViewTemplate>)
Every time my detail table binds [See Detail Table in Bold above]  radgrid inside Nested view template is getting called.
To resolve that i've tried with this , please see below:
protected void grdDomain_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
        {
            if (Common.Common.CurrentSearchCriteria.BatchList != null)
            {
                GridDataItem item = (GridDataItem)e.DetailTableView.ParentItem;
                int DomainID = Convert.ToInt32(item.GetDataKeyValue("DomainID").ToString());
                e.DetailTableView.DataSource = GetResultForCurrentSearchCrietria(Common.Common.CurrentSearchCriteria, null, "SENDER", DomainID);



                RadGrid Grid = (RadGrid)e.DetailTableView.FindControl("grdsearchresult");
                //Grid is always Null/
                if (null != Grid)
                {
                    Grid.NeedDataSource += new GridNeedDataSourceEventHandler(grdsearchresult_NeedDataSource);
                    Grid.ItemDataBound += new GridItemEventHandler(this.grdsearchresult_ItemDataBound);
                    Grid.PreRender += new EventHandler(this.grdsearchresult_PreRender);
                    Grid.SortCommand += new GridSortCommandEventHandler(grdsearchresult_SortCommand);
                    Grid.ItemCreated += new GridItemEventHandler(grdsearchresult_ItemCreated);
                }



            }
        }
But Still I am not able to find internal radgrid inside  grdDomain_DetailTableDataBind. Grid is always null
Can you suggest a way so that i can bind the radgrid inside the nested viewtemplate on demand?
0
Veli
Telerik team
answered on 19 Jul 2010, 11:52 AM
Hello Anoop,

You can use a different approach. If your items are collapsed by default (only the topmost master items are visible) and you are using ServerOnDemand binding, you can use the parent grid's ItemCommand event to check if an "ExpandCollapse" command is initiated. If an item is expanded, this means it is time to bind the nested grid inside.

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    //if expand command and item is not expanded yet (meaning it is to be expanded)
    //and item belongs to the nested GridTableView (not the MasterTableView)
    //it means the child item has the inner RadGrid
    if (e.CommandName == RadGrid.ExpandCollapseCommandName &&
        !e.Item.Expanded &&
        e.Item.OwnerTableView != e.Item.OwnerTableView.OwnerGrid.MasterTableView)
    {
        GridDataItem item = (GridDataItem)e.Item;
        ((RadGrid)item.ChildItem.FindControl("grdsearchresult")).Rebind();
    }
}

The above code will explicitly rebind the nested RadGrid the moment an item from the second level table of the parent grid is expanded. This means the inner grid will be rebound just before it is shown.

Greetings,
Veli
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Eric
Top achievements
Rank 1
Answers by
Eric
Top achievements
Rank 1
Veli
Telerik team
Anoop
Top achievements
Rank 1
Share this question
or