RadTreeList with nested in RadGrid in TemplateColumn

6 posts, 0 answers
  1. John
    John avatar
    14 posts
    Member since:
    Jun 2013

    Posted 01 Aug 2013 Link to this post

    Hi,

    I'm trying to setup a RadTreeList with a nested RadGrid in one of the columns using a TemplateColumn. I've had a look at some of the demo's on the Telerik site with nested grids but I haven't been able to figure out how to pass the Key Id of the TreeList row to the Grid so the correct data is loaded into the grid. I'm using a SQLDataSource like some of the examples on the telerik demo's which uses a session parameter but I haven't figured out how this is set? (no sessions are set in the code behind in the demo's)

    Any help would be great,

    Thanks,
    JC
  2. Angel Petrov
    Admin
    Angel Petrov avatar
    1007 posts

    Posted 06 Aug 2013 Link to this post

    Hello John,

    Thank you for contacting us.

    For loading data into the grid in the particular scenario you can use the NeedDataSource event and execute a query using managed code. For your convenience I have prepared a sample project which demonstrates a possible realization. Please review the example and tell us if this is what you were looking for.

    Let me know if further assistance is required.

    Regards,
    Angel Petrov
    Telerik
    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 the blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Andy
    Andy avatar
    27 posts
    Member since:
    Nov 2010

    Posted 10 Jan 2014 Link to this post

    I would like to do something similar with the RadGrid in a DetailTemplate instead of a Template Column.
    The problem I have is that in the rg_NeedDataSource the NamingContainer  item is a TreeListDetaiTemplateItem which does not have the GetDataKeyValue method.  Is there any way to get this key value so I can populate my datagrid?
  5. Angel Petrov
    Admin
    Angel Petrov avatar
    1007 posts

    Posted 15 Jan 2014 Link to this post

    Hi Andy,

    Indeed you can access the TreeListDataItem related to the DetailTemplate. If we refer to the previous sample and modify the markup like this:

    ASPX:
    <telerik:RadTreeList runat="server" ID="RadTreeList2" AllowPaging="true" PageSize="1" DataKeyNames="EmployeeID"
                ParentDataKeyNames="ReportsTo" DataSourceID="SqlDataSource1">
                <DetailTemplate>
                    <telerik:RadGrid runat="server" ID="RadGrid1" PageSize="2" AllowPaging="true" OnNeedDataSource="RadGrid1_NeedDataSource"></telerik:RadGrid>
                </DetailTemplate>
                <Columns>
                    <telerik:TreeListTemplateColumn HeaderStyle-Width="600px" UniqueName="TemplateColumn">
                        <ItemTemplate>
                           .....
                        </ItemTemplate>
                    </telerik:TreeListTemplateColumn>
                </Columns>
              </telerik:RadTreeList>

    You can modify the NeedDataSource event handler in the code-behind like demonstrated below:
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            RadGrid grid = sender as RadGrid;
            TreeListDataItem item = (grid.NamingContainer as TreeListDetailTemplateItem).ParentItem;
            string keyValue = item.GetDataKeyValue("EmployeeID").ToString();
            grid.DataSource=GetDataTable(string.Format("Select * From Orders Where EmployeeID='{0}'",keyValue));
        }


    Regards,
    Angel Petrov
    Telerik
    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 the blog feed now.
  6. Chrisbarm
    Chrisbarm avatar
    5 posts
    Member since:
    Nov 2008

    Posted 04 Jun 2015 Link to this post

    Hi,

    I have a similar problem, I have a nested RadGrid within a TreeListTemplateColumn,  I need to bind the RadGrid to an ID that is contained in a hidden label within the same templatecolumn.  I can do this fine using the RadTreeList.OnDataBound event but I feel that I should be using the RadGrid NeedDataSource event instead.  I have tried using the DataKeyNames, I need both the DataKeyNames and ParentDataKeyNames to maintain parent child relationship.

    Any help would be appreciated.

    <telerik:RadComboBox ID="RadComboBoxProduct" runat="server" MarkFirstMatch="true" Width="300" Label="Part Number: "
        EnableLoadOnDemand="true" OnItemsRequested="RadComboBoxProduct_ItemsRequested"
        EmptyMessage="Select Part Number" Height="300" AutoPostBack="true">
    </telerik:RadComboBox>
     
    <telerik:RadTreeList ID="RadTreeList1" runat="server" OnNeedDataSource="RadTreeList1_NeedDataSource" OnPreRender="RadTreeList1_PreRender" AllowMultiItemEdit="true" EditMode="InPlace"
        OnItemCreated="RadTreeList1_ItemCreated" ParentDataKeyNames="Parent_Id" DataKeyNames="Template_Id" AutoGenerateColumns="false">
        <Columns>
            <telerik:TreeListTemplateColumn UniqueName="PartNumber" HeaderText="Part Number">
                <ItemTemplate>
                    <%#Eval("PartNumber") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <%#Eval("PartNumber") %>
                </EditItemTemplate>
                <ItemStyle Width="200px" />
            </telerik:TreeListTemplateColumn>
     
            <telerik:TreeListTemplateColumn UniqueName="Description" HeaderText="Description">
                <ItemTemplate>
                    <%#Eval("Description") %>
                </ItemTemplate>
                <EditItemTemplate><%#Eval("Description") %></EditItemTemplate>
            </telerik:TreeListTemplateColumn>
     
            <telerik:TreeListTemplateColumn UniqueName="SerialNumber" HeaderText="SerialNumber">
                <ItemTemplate>
                    <asp:TextBox ID="txtSerialNumber" runat="server" Text='<%#Eval("SerialNumber") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtSerialNumber" runat="server" Text='<%#Eval("SerialNumber") %>' />
                </EditItemTemplate>
                <ItemStyle Width="200px" />
            </telerik:TreeListTemplateColumn>
     
            <telerik:TreeListTemplateColumn UniqueName="Revision" HeaderText="Revision">
                <ItemTemplate>
                    <asp:TextBox ID="txtRevision" runat="server" Text='<%#Eval("Revision") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtRevision" runat="server" Text='<%#Eval("Revision") %>' />
                </EditItemTemplate>
                <ItemStyle Width="200px" />
            </telerik:TreeListTemplateColumn>
     
            <telerik:TreeListTemplateColumn UniqueName="PartNumberDetailsId" HeaderText="Firmware" DataField="PartNumberDetails_Id">
                <ItemTemplate>
                    <asp:Label ID="lblPartNumberDetailsId" runat="server" Text='<%#Eval("PartNumberDetails_Id") %>' Visible="true" />
     
                                <telerik:RadGrid ID="RGFirmware" runat="server" Visible="true" AutoGenerateColumns="false" ShowHeader="false" ShowFooter="false"
                        MasterTableView-EditMode="InPlace" AllowAutomaticInserts="true" OnNeedDataSource="RGFirmware_NeedDataSource">
                        <MasterTableView CommandItemDisplay="bottom">
                            <CommandItemSettings AddNewRecordText="Add Firmware" ShowAddNewRecordButton="false" ShowRefreshButton="false" />
                            <CommandItemTemplate><asp:LinkButton ID="AddFirmware" runat="server" Text="Add Firmware" CommandName="InitInsert" /> </CommandItemTemplate>
                            <NoRecordsTemplate></NoRecordsTemplate>
                            <Columns>
                                <telerik:GridTemplateColumn UniqueName="FirmWare">
                                    <ItemTemplate>
                                        <asp:TextBox ID="txtFirmware" runat="server" Text='<%#Eval("FirmWare") %>' />
     
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtFirmware" runat="server" Text='<%#Eval("FirmWare") %>' />
     
                                    </EditItemTemplate>
     
                                </telerik:GridTemplateColumn>
                            </Columns>
                        </MasterTableView>
                    </telerik:RadGrid>
     
                </ItemTemplate>
            </telerik:TreeListTemplateColumn>
        </Columns>
    </telerik:RadTreeList>

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;
    using Telerik.Web.UI;
     
    public partial class Default3 : System.Web.UI.Page
    {
     
        protected void Page_Load(object sender, EventArgs e)
        {
        }
     
     
        protected void RadComboBoxProduct_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            DataTable objDT = new DataTable("MyTable");
            List<SqlParameter> parameters = new List<SqlParameter>();
            objDT = DataAccess.BuildDataTable("SelectTemplates", parameters);
            RadComboBoxProduct.DataSource = objDT.DefaultView;
            RadComboBoxProduct.DataTextField = "PartNumber";
            RadComboBoxProduct.DataValueField = "Template_Id";
            RadComboBoxProduct.DataBind();
        }
     
     
     
        protected void RadTreeList1_PreRender(object sender, EventArgs e)
        {
            RadTreeList1.ExpandAllItems();
        }
     
     
        protected void RadTreeList1_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
        {
            string TemplateID = "0";
     
            if (!string.IsNullOrEmpty(RadComboBoxProduct.SelectedValue.ToString()))
            { TemplateID = RadComboBoxProduct.SelectedValue.ToString(); }
     
            DataTable objDT = new DataTable("PartNumber");
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@TemplateId", TemplateID));
            objDT = DataAccess.BuildDataTable("SelectPartsForTemplateId", parameters);
            GetChildren(objDT, TemplateID);
            RadTreeList1.DataSource = objDT;
            RadTreeList1.HideExpandCollapseButtonIfNoChildren = true;
            RadTreeList1.Visible = true;
        }
     
     
     
        protected DataTable GetChildren(DataTable objDT, string TemplateID)
        {
            DataTable objDTTemp = new DataTable("TempTable");
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@ParentId", TemplateID));
            objDTTemp = DataAccess.BuildDataTable("SelectPartsForTemplateId", parameters);
            string templateID = string.Empty;
     
            if (objDTTemp.Rows.Count > 0)
            {
                foreach (DataRow objDR in objDTTemp.Rows)
                {
                    objDT.ImportRow(objDR);
                    templateID = objDR["Template_Id"].ToString();
                    GetChildren(objDT, templateID);
                }
            }
     
            return objDT;
        }
     
     
        protected void RadTreeList1_ItemCreated(object sender, TreeListItemCreatedEventArgs e)
        {
            //hide the expand button
            if (e.Item is TreeListDataItem)
            {
                Control expandButton = e.Item.FindControl("ExpandCollapseButton");
                if (expandButton != null)
                {
                    expandButton.Visible = false;
                }
            }
     
        }
     
        protected void RGFirmware_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
     
            RadGrid grid = sender as RadGrid;
            TreeListDataItem item = grid.Parent.Parent as TreeListDataItem;
            string keyValue = ((Label)item["PartNumberDetailsId"].FindControl("lblPartNumberDetailsId")).Text.ToString();
            Response.Write("-" + item["PartNumberDetailsId"].ToString());
     
     
            DataTable objDT = new DataTable("TempTable");
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@PartNumberDetailsId", "1"));
            objDT = DataAccess.BuildDataTable("SelectFirmwareForPart", parameters);
            grid.DataSource = objDT;
     
        }

  7. Angel Petrov
    Admin
    Angel Petrov avatar
    1007 posts

    Posted 09 Jun 2015 Link to this post

    Hi,

    I am not sure about which values you want to access but if you want to access the DataKeyValue and Label value of the parent item you should be able to do so by modifying the NeedDataSource handler as demonstrated below.

    C#:
    protected void RGFirmware_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid grid = sender as RadGrid;
        TreeListDataItem item = grid.Parent.Parent as TreeListDataItem;
        string parentKey;
        string parentTemplate_Id;
        if (item.ParentItem != null)
        {
            parentKey = ((Label)item.ParentItem["PartNumberDetailsId"].FindControl("lblPartNumberDetailsId")).Text;
            parentTemplate_Id=item.ParentItem.GetDataKeyValue("Template_Id").ToString();
        }
        string keyValue = ((Label)item["PartNumberDetailsId"].FindControl("lblPartNumberDetailsId")).Text;
        ...
    }


    Regards,
    Angel Petrov
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017