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

Binding a radgrid inside a GridTemplateColumn in a Radgrid.

6 Answers 324 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Neil
Top achievements
Rank 1
Neil asked on 09 May 2012, 09:53 AM
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

6 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 09 May 2012, 11:11 AM
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
0
Neil
Top achievements
Rank 1
answered on 09 May 2012, 11:36 AM
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
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 10 May 2012, 06:52 AM
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
0
Neil
Top achievements
Rank 1
answered on 10 May 2012, 10:00 AM
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
0
Manish
Top achievements
Rank 2
answered on 17 Jun 2020, 05:15 PM
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?
0
Manish
Top achievements
Rank 2
answered on 17 Jun 2020, 05:15 PM
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?
Tags
Grid
Asked by
Neil
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Neil
Top achievements
Rank 1
Manish
Top achievements
Rank 2
Share this question
or