Binding a radgrid inside a GridTemplateColumn in a Radgrid.

7 posts, 1 answers
  1. Neil
    Neil avatar
    4 posts
    Member since:
    May 2012

    Posted 09 May 2012 Link to this post

    Hi there,
     
    First post on this forum :)

    My question relates to placing a radgrid inside a GridTemplateColumn within a RadGrid, I'm not sure how to do it.  Currently i have a radgrid as per below:

     <telerik:RadGrid id="rgdListLicences" runat="server" Width="100%"  GridLines="None" Skin="eLicensing_paging"  EnableEmbeddedSkins="false" EnableViewState="true">
                         <MasterTableView RetrieveAllDataFields="false" AutoGenerateColumns="false" >
                            <Columns>
                                <telerik:GridBoundColumn DataField="clientRef" HeaderText="Client Ref">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="commencementDate" HeaderText="Commencement Date">
                                </telerik:GridBoundColumn>
                                <telerik:GridTemplateColumn UniqueName="validations" HeaderText="Validations" SortExpression="CompanyName" InitializeTemplatesFirst="false">
                                    <ItemTemplate>
                                        <telerik:radgrid ID="rgdValidations"  runat="server" >
                                            <MasterTableView >
                                                <Columns >
                                                    <telerik:GridBoundColumn DataField="validations.Text">
                                                    </telerik:GridBoundColumn>
                                                </Columns>
                                            </MasterTableView>
                                        </telerik:radgrid>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>

                            </Columns>
                        </MasterTableView>
                    </telerik:RadGrid>


    And I Bind to the radgrid in the codebehind, as follows:

    rgdListLicences.DataSource = CType(licenceImportParser.getLicences.Licence.ToList, IEnumerable)
    rgdListLicences.DataBind()


    The Datasource licenceImportParser.getLicences.Licence, is a list of objects.  These objects contain several properties, which bind successfully to the other columns (ie. commencement date above), but also contain a list of Strings called validations which i need to bind to the radgrid inside the GridTemplateColumn.

    I thought i could just set the DataField="validations.Text"  or something like that considering validations is a list of Strings.. but not sure here!


    But I cant figure out how i can bind it!  Any help is greatly appreciated!!

    Thanks
    Neil
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 09 May 2012 Link to this post

    Hello Neil ,

      <MasterTableView DataKeyNames="ID">
     <Columns>
      <telerik:GridTemplateColumn>
                            <ItemTemplate>
                                <telerik:RadGrid ID="RadGrid2" runat="server" OnNeedDataSource="RadGrid2_NeedDataSource" ></telerik:RadGrid>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
    ............
    .............
    protected void RadGrid2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
     
            RadGrid RadGrid2 = sender as RadGrid;
            GridDataItem item = RadGrid2.NamingContainer as GridDataItem;
            string strKey = item.GetDataKeyValue("ID").ToString();
     
     
     
            dynamic data = new[] {
                    new { Name ="Name_" +strKey}
                };
     
            RadGrid2.DataSource = data;
        }
     
     
     
     protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
           
            if (e.Item is GridDataItem)
            {
                GridDataItem item = e.Item as GridDataItem;
                RadGrid RadGrid2 = item.FindControl("RadGrid2") as RadGrid;
                RadGrid2.Rebind();
            }
     
    }


    Thanks,
    Jayesh Goyani
  3. Neil
    Neil avatar
    4 posts
    Member since:
    May 2012

    Posted 09 May 2012 Link to this post

    Hi Jayesh,

    Thanks alot for the answer, I am nearly there.  But in your example you bind the Data Key Value "ID" from the parent grid to the child grid.  I need to be able to access a list of strings which is contain withing the object that is bound to the parent grid?

    Is this possible?
    Thanks
    Neil
  4. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 10 May 2012 Link to this post

    Hello Neil,

    You can also assign multiple field in DataKey.
    <MasterTableView DataKeyNames="ID,Name,Profile">

    GridDataItem item = RadGrid2.NamingContainer as GridDataItem;
            string strKey = item.GetDataKeyValue("ID").ToString();
           string strName = item.GetDataKeyValue("Name").ToString();


    // You can also use parent's column also.
    <MasterTableView>
    <Columns>
    <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                       </telerik:GridBoundColumn>
                       <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name" >
                       </telerik:GridBoundColumn>

    GridDataItem item = RadGrid2.NamingContainer as GridDataItem;
            string strKey = item["ID"].text;
           string strName =  item["Name"].text;



    Thanks,
    Jayesh Goyani
  5. Neil
    Neil avatar
    4 posts
    Member since:
    May 2012

    Posted 10 May 2012 Link to this post

    I see what you mean Jayesh thanks!


    I ended up using your method but with a slight variation on the needdatasource for the inner grid.


    Protected Sub rgdValidations_NeedDataSource(ByVal sender As Object, ByVal e As GridNeedDataSourceEventArgs)
     
        Dim RadGrid2 As RadGrid = CType(sender, RadGrid)
        Dim item As GridDataItem = RadGrid2.NamingContainer
        Dim validations As List(Of String) = CType(item.DataItem, LicencesLicence).validations
     
        RadGrid2.DataSource = validations
    End Sub
  6. Manish
    Manish avatar
    74 posts
    Member since:
    Jul 2011

    Posted 17 Jun 2020 Link to this post

    Hi your post helps me alot but I need to fix another problem if I want to enable paging in RadGrid2 than how can I do that. Can you please help me?
  7. Manish
    Manish avatar
    74 posts
    Member since:
    Jul 2011

    Posted 17 Jun 2020 in reply to Jayesh Goyani Link to this post

    Hi your post helps me alot but I need to fix another problem if I want to enable paging in RadGrid2 than how can I do that. Can you please help me?
Back to Top