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

Nested Grid is lost when click edit of parent grid

4 Answers 77 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sterlin Swan
Top achievements
Rank 1
Sterlin Swan asked on 31 May 2012, 05:52 PM
Hi,

I have a grid which contains a nested grid.  When the page loads I expand all the items by using <MasterTableView HierarchyDefaultExpanded="true"
When clicking the edit link on the parent grid all of the child grids vanish but the grid is still in an expanded state for all records.  The only way to get the child grids back is to click the Expand/Collapse on one row, then they all appear again.
What I would like to achieve is to be able to edit a parent record and keep all child grids visible for the other records.  Thanks for your help.

4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 31 May 2012, 06:54 PM
Hello,

I am not able to reproduce the issue, Please check below code snippet.
<telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" AutoGenerateColumns="false"
           AllowPaging="true" OnNeedDataSource="RadGrid1_NeedDataSource" OnDetailTableDataBind="RadGrid1_DetailTableDataBind">
           <MasterTableView DataKeyNames="ID" HierarchyDefaultExpanded="true" HierarchyLoadMode="Client">
               <Columns>
                    
                   <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                   </telerik:GridBoundColumn>
                   <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
               </Columns>
               <DetailTables>
                   <telerik:GridTableView>
                       <Columns>
                           <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                           </telerik:GridBoundColumn> <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
                       </Columns>
                   </telerik:GridTableView>
               </DetailTables>
           </MasterTableView>
       </telerik:RadGrid>
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            RadGrid1.DataSource = (DataTable)Session["mydata"];
        }
 
        protected void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
        {
            e.DetailTableView.DataSource = (DataTable)Session["mydata"];
        }
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("ID", typeof(System.Int32));
                dt.Columns.Add("Name", typeof(System.String));
                dt.Rows.Add(1, "name1");
                dt.Rows.Add(2, "name2");
                dt.Rows.Add(3, "name3");
 
                Session["mydata"] = dt;
            }
        }


Thanks,
Jayesh Goyani
0
Sterlin Swan
Top achievements
Rank 1
answered on 31 May 2012, 07:43 PM
I will have a look at your code, thanks.  Here is what I had:

<telerik:RadGrid ID="RadGrid2" runat="server" GridLines="None" AutoGenerateColumns="False"
                                    AllowPaging="True" AllowSorting="True" PageSize="6" CellPadding="2">
                                    <MasterTableView HierarchyDefaultExpanded="True" DataKeyNames="TopLevelID">
                                        <NestedViewTemplate>
                                            <asp:Panel ID="InnerContainer" runat="server">
                                            <asp:Label runat="server" Text="test"></asp:Label>
                                                <telerik:RadGrid ID="RadGrid3" runat="server" AutoGenerateColumns="false">
                                                    <MasterTableView ShowHeader="false">
                                                        <Columns>
                                                            <telerik:GridTemplateColumn UniqueName="VInfo" InitializeTemplatesFirst="false">
                                                                <ItemTemplate>
                                                                    <table cellpadding="2" cellspacing="0">
                                                                        <tr>
                                                                            <td>
                                                                                <table>
                                                                                    <tr>
                                                                                        <td>
                                                                                            Type:</td>
                                                                                        <td>
                                                                                            <%# Eval("Type")%>
                                                                                        </td>
                                                                                    </tr>
                                                                                </table>
                                                                            </td>
                                                                        </tr>
                                                                        <tr>
                                                                            <td>
                                                                                <table>
                                                                                    <tr>
                                                                                        <td>
                                                                                            First Name:</td>
                                                                                        <td>
                                                                                            <%# Eval("Name")%>
                                                                                        </td>
                                                                                        <td>
                                                                                            Last Name:</td>
                                                                                        <td colspan="3">
                                                                                            <%# Eval("LastName")%>
                                                                                        </td>
                                                                                    </tr>
                                                                                      
                                                                                </table>
                                                                            </td>
                                                                        </tr>
                                                                        <tr>
                                                                            <td>
                                                                                <table>
                                                                                    <tr>
                                                                                        <td>
                                                                                            Comments:</td>
                                                                                        <td>
                                                                                            <%# Eval("Comments")%>
                                                                                        </td>
                                                                                    </tr>
                                                                                </table>
                                                                            </td>
                                                                        </tr>
                                                                    </table>
                                                                </ItemTemplate>
                                                                <EditItemTemplate>
                                                                    <table cellpadding="2" cellspacing="0">
                                                                        <tr>
                                                                            <td>
                                                                                <table>
                                                                                    <tr>
                                                                                        <td>
                                                                                            Comments:</td>
                                                                                        <td>
                                                                                            <asp:TextBox ID="txtComments" Rows="3" TextMode="MultiLine" runat="server" Text='<%# Eval("Comments") %>'
                                                                                                Width="500px"></asp:TextBox>
                                                                                        </td>
                                                                                    </tr>
                                                                                </table>
                                                                            </td>
                                                                        </tr>
                                                                    </table>
                                                                </EditItemTemplate>
                                                            </telerik:GridTemplateColumn>
                                                            <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn1" />
                                                        </Columns>
                                                    </MasterTableView>
                                                </telerik:RadGrid>
                                            </asp:Panel>
                                        </NestedViewTemplate>
                                        <Columns>
                                            <telerik:GridBoundColumn DataField="TopLevelID" UniqueName="TopLevelID"
                                                HeaderText="TopLevelViolationID" ReadOnly="True">
                                                <HeaderStyle Width="100px" />
                                            </telerik:GridBoundColumn>
                                            <telerik:GridBoundColumn DataField="Account" UniqueName="Account" HeaderText="Account" ReadOnly="True">
                                                <HeaderStyle Width="100px" />
                                            </telerik:GridBoundColumn>
                                             
                                            <telerik:GridTemplateColumn UniqueName="Notes" HeaderText="Notes" InitializeTemplatesFirst="false">
                                             <HeaderStyle Width="700px" />
                                                <ItemTemplate>
                                                    <asp:Label ID="lblNotes" runat="server" Text=<%# Eval("Notes")%>></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtNotes" runat="server" Text=<%# Eval("Notes")%> Rows=3 TextMode="MultiLine" Width="500"></asp:TextBox>
                                                </EditItemTemplate>
                                            </telerik:GridTemplateColumn
                                            <telerik:GridEditCommandColumn>
                                            </telerik:GridEditCommandColumn>
                                        </Columns>
                                        <ExpandCollapseColumn Visible="True">
                                        </ExpandCollapseColumn>
                                    </MasterTableView>
                                    <GroupingSettings CaseSensitive="False" />
                                </telerik:RadGrid>



 

Private Sub RadGrid2_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid2.ItemCreated
        If TypeOf e.Item Is GridNestedViewItem Then
              
  
            e.Item.FindControl("InnerContainer").Visible = (DirectCast(e.Item, GridNestedViewItem)).ParentItem.Expanded
  
            Dim RadGrid3 As Telerik.Web.UI.RadGrid = DirectCast(e.Item.FindControl("RadGrid3"), RadGrid)
              
  
            AddHandler RadGrid3.NeedDataSource, AddressOf Me.RadGrid3_NeedDataSource
            AddHandler RadGrid3.ItemCommand, AddressOf Me.RadGrid3_ItemCommand
  
             
        End If
  
    End Sub
  
Private Sub RadGrid2_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid2.ItemCommand
        If e.CommandName = RadGrid.ExpandCollapseCommandName And TypeOf e.Item Is GridDataItem Then
            DirectCast(e.Item, GridDataItem).ChildItem.FindControl("InnerContainer").Visible = Not e.Item.Expanded
  
            Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
            If e.Item.Expanded = False Then
                  
  
                Dim nestedItem As GridNestedViewItem = DirectCast(item.ChildItem, GridNestedViewItem)
                Dim RadGrid3 As Telerik.Web.UI.RadGrid = DirectCast(nestedItem.FindControl("RadGrid3"), RadGrid)
  
                RadGrid3.Rebind()
            End If
         
        End If
  
         
  
    End Sub
  
Private Sub RadGrid2_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid2.NeedDataSource
        GetTopLevelViolations()
    End Sub
  
 Private Sub GetTopLevelViolations()
  
        RadGrid2.DataSource = TopLevelBL.GetTopLevel
  
    End Sub
  
   
  
   
  
Private Sub RadGrid3_NeedDataSource(ByVal source As Object, ByVal e As GridNeedDataSourceEventArgs)
         
  
        Dim nestedItem As GridNestedViewItem = DirectCast(TryCast(source, RadGrid).NamingContainer, GridNestedViewItem)
  
        Dim dataKeyValue As Object = nestedItem.ParentItem.GetDataKeyValue("TopLevelID")
  
   
  
        Dim RadGrid3 = DirectCast(source, RadGrid)
  
        RadGrid3.DataSource = VBL.GetData(dataKeyValue)
  
   
  
    End Sub
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 31 May 2012, 08:11 PM
Hello,

please check below code snippet.
<telerik:RadGrid ID="RadGrid2" runat="server" GridLines="None" AutoGenerateColumns="False"
         AllowPaging="True" AllowSorting="True" PageSize="6" CellPadding="2" OnNeedDataSource="RadGrid2_NeedDataSource"
         AllowMultiRowEdit="true">
         <MasterTableView HierarchyDefaultExpanded="True" DataKeyNames="ID">
             <NestedViewTemplate>
                 <asp:Panel ID="InnerContainer" runat="server">
                     <asp:Label ID="Label1" runat="server" Text="test"></asp:Label>
                     <telerik:RadGrid ID="RadGrid3" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid3_NeedDataSource">
                         <MasterTableView ShowHeader="false">
                             <Columns>
                                 <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                                 </telerik:GridBoundColumn>
                                 <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                                 </telerik:GridBoundColumn>
                                 <telerik:GridTemplateColumn>
                                     <ItemTemplate>
                                         child
                                     </ItemTemplate>
                                 </telerik:GridTemplateColumn>
                                 <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn1" />
                             </Columns>
                         </MasterTableView>
                     </telerik:RadGrid>
                 </asp:Panel>
             </NestedViewTemplate>
             <Columns>
                 <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                 </telerik:GridBoundColumn>
                 <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                 </telerik:GridBoundColumn>
                 <telerik:GridTemplateColumn>
                     <ItemTemplate>
                         parent
                     </ItemTemplate>
                 </telerik:GridTemplateColumn>
                 <telerik:GridEditCommandColumn>
                 </telerik:GridEditCommandColumn>
             </Columns>
             <ExpandCollapseColumn Visible="True">
             </ExpandCollapseColumn>
         </MasterTableView>
         <GroupingSettings CaseSensitive="False" />
     </telerik:RadGrid>
Protected Sub RadGrid2_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs)
    Dim data As dynamic = New () {New With { _
        .ID = 1, _
        .Name = "name1" _
    }, New With { _
        .ID = 2, _
        .Name = "name2" _
    }, New With { _
        .ID = 3, _
        .Name = "name3" _
    }, New With { _
        .ID = 4, _
        .Name = "Name4" _
    }, New With { _
        .ID = 5, _
        .Name = "name5" _
    }}
 
    RadGrid2.DataSource = data
End Sub
 
Protected Sub RadGrid3_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs)
    Dim RadGrid3 As RadGrid = TryCast(sender, RadGrid)
 
    Dim strID As String = TryCast(RadGrid3.NamingContainer, GridNestedViewItem).ParentItem.GetDataKeyValue("ID").ToString()
 
    Dim data As dynamic = New () {New With { _
        .ID = 1, _
        .Name = "name_" + strID _
    }, New With { _
        .ID = 2, _
        .Name = "name_" + strID _
    }, New With { _
        .ID = 3, _
        .Name = "name_" + strID _
    }, New With { _
        .ID = 4, _
        .Name = "Name_" + strID _
    }, New With { _
        .ID = 5, _
        .Name = "name_" + strID _
    }}
 
    RadGrid3.DataSource = data
End Sub

Let me know if any concern.

Thanks,
Jayesh Goyani
0
Sterlin Swan
Top achievements
Rank 1
answered on 31 May 2012, 09:00 PM

Thank you Jayesh.  Simply putting OnNeedDataSource="RadGrid2_NeedDataSource" and OnNeedDataSource="RadGrid3_NeedDataSource"

in the grid declaration in the aspx page solvd the problem.

Tags
Grid
Asked by
Sterlin Swan
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Sterlin Swan
Top achievements
Rank 1
Share this question
or