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

Accessing a radGrid that is nested inside another radGrid

7 Answers 611 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aye Myint
Top achievements
Rank 1
Aye Myint asked on 10 Feb 2010, 08:48 PM

Hi,

I want to be able to access a radGridthat is nested inside a radGrid then populate the nested radGrid from theOnEditCommand of the parent radGrid.

 

For example: radGrid1 containsradGrid2 within the EditFormSettings.FormTemplate. OnItemCommand, I want todisplay custom formatting for my edit form as well as a new radGrid with otherinformation. Is this possible?

 

I have tried using thee.item.findControl(“radGrid2”) from the GridCommandEventArgs, but null isreturned. In fact, any object I have within the EditFormSettings.FormTemplateseems to be inaccessible using this syntax. Since this didn’t work, I triediterating through the radGrid1’s items and searched for theGridItemType.EditFormItem and tried locating radGrid2 that way, but also wasunsuccessful.

protected void RadGrid1_OnEditCommand( object sender, GridCommandEventArgs e )  
        {  
            RadGrid masterGrid = (RadGrid) sender;  
            foreach ( GridItem gi in masterGrid.Items )  
            {  
                if ( gi.ItemType == GridItemType.EditFormItem )  
                {  
                    TextBox textBox1 = (TextBox) e.Item.FindControl( "txtCreditorName" );  
                    RadGrid radGrid2 = (RadGrid) e.Item.FindControl( "RadGrid2" );                      
                }  
            }  
        }  
 

 

 I looked into the thread whichis talking about <NestedViewTemplate> but that is different from what Iwanted to do.

Can you please take a look at it? Anyhelp would be greatly appreciated.

 

 Here is  my code in aspxpage.

            <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowSorting="True" Width="100%"  
                AutoGenerateColumns="False" GridLines="None" Height="100%" onneeddatasource="RadGrid1_NeedDataSource" 
                onupdatecommand="RadGrid1_UpdateCommand"  oneditcommand="RadGrid1_EditCommand" onupdatecommand="RadGrid1_InsertCommand"
                 
            <PagerStyle Mode="NumericPages" AlwaysVisible="True" />                
            <MasterTableView CommandItemDisplay="Top" Width="100%" AllowMultiColumnSorting="True" EditMode="PopUp" 
                DataKeyNames="creditor_id" Name="creditor">    
                 
                 <Columns>                     
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn"
                        <HeaderStyle Width="30px" /> 
                        <ItemStyle CssClass="MyImageButton" /> 
                    </telerik:GridEditCommandColumn> 
                    <telerik:GridBoundColumn SortExpression="creditor_id" HeaderButtonType="TextButton" DataField="creditor_id" 
                        HeaderText="Creditor Id" UniqueName="creditor_id" ReadOnly="true" Display="false"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="Name" HeaderButtonType="TextButton" DataField="Name" HeaderText="Creditor Name" 
                        UniqueName="Name"
                    </telerik:GridBoundColumn> 
 
                </Columns>                  
                        
 
           <EditFormSettings EditFormType="Template" PopUpSettings-Height="300px" PopUpSettings-Width="450px"
                     
                    <EditColumn UniqueName="EditCommandColumn1"
 
                    </EditColumn> 
                    <FormTemplate> 
                        <table id="tblCreditor" cellspacing="5" cellpadding="1" width="450" border="0" rules="none"
                            <tr> 
                                <td> 
                                    Creditor Name: 
                                </td> 
                                <td> 
                                    <asp:TextBox ID="txtCreditorName" runat="server" Text='<%# Bind( "Name") %>'</asp:TextBox> 
                                </td> 
                            </tr> 
 
                            <tr> 
                                <td> 
                                     
                                   <telerik:RadGrid ID="RadGrid2" runat="server" Width="100%"
 
                                    <PagerStyle Mode="NumericPages" AlwaysVisible="True" />                
                                    <MasterTableView CommandItemDisplay="Top" Width="100%" AllowMultiColumnSorting="True"  
                                        DataKeyNames="person_id" Name="contact"
 
                                         <Columns> 
                                            <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn2"
                                                <HeaderStyle Width="30px" /> 
                                                <ItemStyle CssClass="MyImageButton" /> 
                                            </telerik:GridEditCommandColumn> 
                                            <telerik:GridBoundColumn SortExpression="person_id" HeaderText="Person Id" HeaderButtonType="TextButton" 
                                                DataField="person_id" UniqueName="person_id" ReadOnly="true" Display="false"
                                            </telerik:GridBoundColumn> 
                                            <telerik:GridBoundColumn SortExpression="Name" HeaderText="Contact Name" HeaderButtonType="TextButton" 
                                                DataField="Name" UniqueName="Name"
                                            </telerik:GridBoundColumn> 
                                            <telerik:GridBoundColumn SortExpression="Description" HeaderText="Description" HeaderButtonType="TextButton" 
                                                DataField="Description" UniqueName="Description"
                                            </telerik:GridBoundColumn> 
                                        </Columns> 
                    
                                    </MasterTableView> 
                                  </telerik:RadGrid>                    
                                                  
                             </td> 
                            </tr> 
                            <tr> 
                                <td align="center" colspan="2"
                                    <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' 
                                        runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' 
                                        ValidationGroup="creditor" TabIndex="7"></asp:Button>&nbsp; 
                                    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel" 
                                        TabIndex="8"
                                    </asp:Button> 
                                </td> 
                            </tr> 
                        </table> 
                    </FormTemplate> 
 
                   <PopUpSettings Height="300px" Width="450px"></PopUpSettings> 
                     
                </EditFormSettings> 
            </MasterTableView> 
            </telerik:RadGrid> 
 



7 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 11 Feb 2010, 05:39 AM
Hi Aye Myint,

You can access the inner radgrid in the ItemDataBound event of parent grid.

C#:
 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
        { 
            GridEditFormItem editItem = (GridEditFormItem)e.Item; 
            TextBox textBox1 = (TextBox)editItem.FindControl("txtCreditorName"); 
            RadGrid radGrid2 = (RadGrid)editItem.FindControl("RadGrid2");             
        } 
    } 

-Shinu.
0
Aye Myint
Top achievements
Rank 1
answered on 12 Feb 2010, 05:10 PM
Hi Shinu,

Thanks for your code. That was really helpful to me.
Now I'm able to display the RadGrid2 inside the edit form of RadGrid1. What I need to do is that RadGrid2 should also have insert, edit and delete buttons. So I put another <EditFormSettings> code blog in my RadGrid2.

When I click on a edit button in RadGrid1, it pop-ups the right edit form. Then in that pop-up form, I have my RadGrid2 with edit buttons. The problem I'm having now is when I click on a edit button in RadGrid2, it pop-ups the RadGrid1's edit form instead of the corresponding one. 

Here is how I databind my RadGrid2.
 
        protected void RadGrid1_ItemDataBound( object sender, GridItemEventArgs e )  
        {  
            if ( e.Item is GridEditableItem && e.Item.IsInEditMode )  
            {  
                GridEditableItem item = e.Item as GridEditableItem;  
  
                RadGrid grid2 = item.FindControl( "RadGrid2" ) as RadGrid;  
  
                if ( grid2 != null )  
                {  
                    var datasource = ( some linq to sql query here );  
                    grid2.DataSource = datasource;  
                    grid2.DataBind();  
                }  
            }  
        } 

What I should do to have my RadGrid2 with the insert, edit and delete abilities from this point?
 Thanks again.

Aye
0
Pavlina
Telerik team
answered on 15 Feb 2010, 01:57 PM
Hello Aye,

Please, consider using advanced binding through the NeedDataSource event of the grid instead of simple binding with DataBind() calls. Have in mind that simple data-binding can be used in simple cases when you do not require the grid to perform complex operations such as: Inserting, deleting, and updating records through custom edit forms (WebUserControl or FormTemplate)

I hope this helps.

Regards,
Pavlina
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
Andrew
Top achievements
Rank 2
answered on 07 Mar 2010, 07:15 AM
I have also posted this on another thread. 

I need to do the same thing.  I need to edit a grid that is inside another grid.  Populating the inner grid is not an issue.  the problem comes when I hit the Edit button.  I cannot gain access to the Object that the row to be edited is bound to. 

Help!!!
0
Pavlina
Telerik team
answered on 10 Mar 2010, 04:30 PM
Hello Andrew,

Please, examine the online resources below, for more information about how to achieve your goal.
Update/Insert/Delete in Hierarchy
Accessing cells and rows

Kind regards,
Pavlina
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
sreelatha bora
Top achievements
Rank 1
answered on 28 Mar 2010, 06:30 AM
Hi, i am also using Radgrid2  inside the formtemplate of Radgrid1 .how to achieve insert/update/delete for the nested grid -Radgrid2.Please help me.

Thanks
Sree
0
Pavlina
Telerik team
answered on 31 Mar 2010, 12:29 PM
Hello Sree,

Go through the forum thread below and see if it helps to achieve your goal:
http://www.telerik.com/community/forums/aspnet-ajax/grid/nestedviewtemplate-example-question.aspx

Regards,
Pavlina
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
Aye Myint
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Aye Myint
Top achievements
Rank 1
Pavlina
Telerik team
Andrew
Top achievements
Rank 2
sreelatha bora
Top achievements
Rank 1
Share this question
or