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

How to add NestedViewTemplate to the grid on page_init in codebehind?

5 Answers 407 Views
Grid
This is a migrated thread and some comments may be shown as answers.
giridhar
Top achievements
Rank 1
giridhar asked on 18 Mar 2009, 05:35 AM
Hi,
  Currently I have a requirement to add the grid control dynamically, this has NestedViewTemplate.
I was able to construct Grid on code behind as RadGrid r = new RadGrid(); How can I add NestedViewTemplate to this grid?

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 18 Mar 2009, 10:24 AM
Hello Giridhar,

You can try out the following code to create a NestedViewTemplate dynamically in the grid:
cs:
 protected void Page_Init(object sender, EventArgs e) 
    { 
        RadGrid grid = new RadGrid(); 
        grid.AutoGenerateColumns = false
        grid.DataSourceID = "SqlDataSource1";        
        grid.MasterTableView.NestedViewTemplate = new MyTemplate();       
       
        GridBoundColumn boundColumn1 = new GridBoundColumn(); 
        boundColumn1.DataField = "ContactName"
        boundColumn1.UniqueName = "ConactName"
        boundColumn1.HeaderText = "Bound Column";         
        grid.MasterTableView.Columns.Add(boundColumn1); 
        grid.AllowPaging = true
        grid.PageSize = 3;         
        PlaceHolder1.Controls.Add(grid); 
    } 
 
    private class MyTemplate : ITemplate 
    { 
         
        protected TextBox textBox;// to add a TextBox control in the NestedViewTemplate 
        
        public void InstantiateIn(System.Web.UI.Control container) 
        {           
            textBox = new TextBox(); 
            textBox.ID = "NestedTextBox";                         
            container.Controls.Add(textBox); 
             
        } 
         
    } 

Thanks
Princy.
0
giridhar
Top achievements
Rank 1
answered on 18 Mar 2009, 11:10 AM
Thanks for the reply Princy, However I looking at following information. Do let me know the possibility.

I have written .aspx with a grid and NestedViewTemplate in grid.

Now I want to retrieve this NestedViewTemplate and add some controls into this template on code behind(basically page_init). So that it works for my requirement.
Explanation with code. Have look at code below.
 
        <telerik:RadGrid ID="searchGrid" runat="server" Skin="Vista" 
         OnNeedDataSource="searchGrid_NeedDataSourceAdvancedSearch" OnColumnCreated="searchGrid_ColumnCreated" 
         OnItemDataBound="searchGrid_ItemDataBound" OnItemCommand="searchGrid_ItemCommand" 
         AutoGenerateColumns="false"
        <MasterTableView> 
            <NestedViewTemplate> 
                <telerik:RadGrid ID="nestedMatchedGrid" runat="server"
                    <MasterTableView> 
                        <Columns> 
                            <telerik:GridTemplateColumn UniqueName="ActivityID" Visible="false"
                                <ItemTemplate> 
                                    <asp:Label ID="activityId" runat="server" Text='<%#Eval("ItemPK")%>'></asp:Label> 
                                </ItemTemplate> 
                            </telerik:GridTemplateColumn> 
                            <telerik:GridTemplateColumn UniqueName="ActivityName" HeaderText="L_Name"
                                <ItemTemplate> 
                                    <asp:Label ID="activityName" runat="server" Text='<%#Eval("ItemName")%>'></asp:Label> 
                                </ItemTemplate> 
                            </telerik:GridTemplateColumn> 
                        </Columns> 
                    </MasterTableView> 
                </telerik:RadGrid> 
                <telerik:RadGrid ID="nestedSelfReferentialGrid" runat="server"
                    <MasterTableView> 
                        <Columns> 
                            <telerik:GridTemplateColumn UniqueName="ActivityID" Visible="false"
                                <ItemTemplate> 
                                    <asp:Label ID="activityId" runat="server" Text='<%#Eval("ItemPK")%>'></asp:Label> 
                                </ItemTemplate> 
                            </telerik:GridTemplateColumn> 
                            <telerik:GridTemplateColumn UniqueName="ActivityName" HeaderText="L_Name"
                                <ItemTemplate> 
                                    <asp:Label ID="activityName" runat="server" Text='<%#Eval("ItemName")%>'></asp:Label> 
                                </ItemTemplate> 
                            </telerik:GridTemplateColumn> 
                        </Columns> 
                    </MasterTableView> 
                </telerik:RadGrid> 
            </NestedViewTemplate> 
           <Columns> 
            <telerik:GridTemplateColumn UniqueName="ActivityID" Visible="false"
                <ItemTemplate> 
                    <asp:Label ID="activityId" runat="server" Text='<%#Eval("ItemPK")%>'></asp:Label> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
            <telerik:GridTemplateColumn UniqueName="ActivityName" HeaderText="L_Name"
                <ItemTemplate> 
                    <asp:Label ID="activityName" runat="server" Text='<%#Eval("ItemName")%>'></asp:Label> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
        </Columns> 
        </MasterTableView> 
    </telerik:RadGrid> 
If you have a look you have two grids in the NestedViewTemplate. I should be able to add additional columns to these grids(one of it is self refrential grid.) in NestedViewTemplate on the code behind.

I was able to add additional columns to the Main Grid(searchGrid) on page_init. Do let me know how  to add additional columns to grids in NestedViewTemplate(nestedMatchedGrid, nestedSelfReferentialGrid).

Please let  me know the possible work around. ANy help would be a great favour.

Thanks and regards,
Giridhar.
0
Princy
Top achievements
Rank 2
answered on 19 Mar 2009, 07:09 AM
Hi Giridhar,

You cannot mix up dynamic column creation and declarative column creation. "RadGrid does not support mixing declarative grid columns with grid columns added dynamically at runtime. You should either create all the columns in the grid programmatically, or else define them all in the ASPX file". For information on this you can take a look at the following document:
Programmatic creation

-Princy.
0
giridhar
Top achievements
Rank 1
answered on 30 Mar 2009, 08:45 AM
Hi Princy,
   As said before, I have RadGrid inside the NestedViewTemplate. As per your post If I instantiate NestedViewTemplate every time when I expand the row, it would create a new grid inside the NestedViewTemplate and when there are post backs related to this grid happen since it is new grid being created all the time It would lose the state.(I have self referential grid in nested view template, there would click events and expand events to be handled even in inner grids.) How can I handle the same. Please do explain with a sample code piece.
0
Manuel Ortiz
Top achievements
Rank 1
answered on 06 Apr 2010, 07:36 AM
Hi Princy, in the first answer you gave above, How can I access the Template's textbox so that I can alter its text when the row is expanded??

Thanx,
Manuel
Tags
Grid
Asked by
giridhar
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
giridhar
Top achievements
Rank 1
Manuel Ortiz
Top achievements
Rank 1
Share this question
or