This is a migrated thread and some comments may be shown as answers.
Grid with Subgrid - Subgrid empty after postback
2 Answers 47 Views
This is a migrated thread and some comments may be shown as answers.
Koen
Top achievements
Rank 1
Koen asked on 22 Jan 2013, 10:46 AM
Dear,

I'm using the Telerik Grid for one of my webprojects.
The grid show data for a purchase order. For a line 2 subgrids can optionally contain data.
When loading the grid for the first time, it works fine.
When I edit a row, i do this with a server postback that shows a modal popup.
If the user saves changes for a line, another server postback happens and the grid is reloaded.
With this reload, only data in the main grid is shown. The subgrids don't show any data.
The need_datasource event is not even fired this time. However the code is exactly the same, so I don't understand what can be wrong.

Can someone help me with this weird issue?
Many thanks in advance!

Best regards, Koen

UI:
<telerik:RadGrid ID="RadGridOrderLines" AutoGenerateColumns="false" AllowPaging="false" runat="server">
    <MasterTableView HierarchyDefaultExpanded="true" DataKeyNames="PurchaseOrderDetailID">
        <Columns>
            <telerik:GridButtonColumn ImageUrl="~/Images/edit_16x16.gif" CommandName="editline" ButtonType="ImageButton" ></telerik:GridButtonColumn>
            <telerik:GridBoundColumn HeaderText="ITE" DataField="ArticleITECode" SortExpression="ArticleITECode"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText='<%$ Resources:CommonTerms, Article %>' DataField="ArticleName" SortExpression="ArticleName"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText='<%$ Resources:CommonTerms, Status %>' DataField="PurchaseOrderDetailStatusDescription" SortExpression="PurchaseOrderDetailStatusDescription"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText='<%$ Resources:CommonTerms, Price %>' DataField="ArticleUnitPrice" DataFormatString="{0:F2}" SortExpression="ArticleUnitPrice"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText='<%$ Resources:CommonTerms, Quantity %>' DataField="PurchaseOrderDetailQuantity" DataFormatString="{0:F0}" SortExpression="PurchaseOrderDetailQuantity"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText='<%$ Resources:CommonTerms, Remark %>' DataField="PurchaseOrderDetailRemarks" SortExpression="PurchaseOrderDetailRemarks"></telerik:GridBoundColumn>
        </Columns>
        <NestedViewTemplate>
            <asp:Panel CssClass="pad5" runat="server" ID="panelDeliveries">
                <asp:Label runat="server" ID="lblDeliveries" Text='<%$ Resources:CommonTerms, Deliveries %>'></asp:Label
                <asp:Label runat="server" ID="lblDeliveriesCount"></asp:Label><br />
                <telerik:RadGrid ID="RadGridDelivery" AutoGenerateColumns="false" AllowPaging="false" runat="server">
                    <MasterTableView>
                        <Columns>
                            <telerik:GridBoundColumn HeaderText="ITE" DataField="ArticleITECode" SortExpression="ArticleITECode"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText='<%$ Resources:CommonTerms, Article %>' DataField="ArticleName" SortExpression="ArticleName"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText='<%$ Resources:CommonTerms, Status %>' DataField="PurchaseDeliveryStatus" SortExpression="PurchaseDeliveryStatus"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText='<%$ Resources:CommonTerms, Price %>' DataField="ArticleUnitPrice" DataFormatString="{0:F2}" SortExpression="ArticleUnitPrice"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText='<%$ Resources:CommonTerms, Quantity %>' DataField="PurchaseDeliveryQuantity" DataFormatString="{0:F0}" SortExpression="PurchaseDeliveryQuantity"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText='<%$ Resources:CommonTerms, Remark %>' DataField="PurchaseDeliveryRemarks" SortExpression="PurchaseDeliveryRemarks"></telerik:GridBoundColumn>
                        </Columns>
                    </MasterTableView>
                </telerik:RadGrid>
            </asp:Panel>
            <asp:Panel runat="server" ID="panelAssets" CssClass="pad5">
                <asp:Label runat="server" ID="lblAssets" Text='<%$ Resources:CommonTerms, Assets %>'></asp:Label
                <asp:Label runat="server" ID="lblAssetsCount"></asp:Label><br />
                <telerik:RadGrid ID="RadGridAssets" AutoGenerateColumns="false" AllowPaging="false" runat="server">
                    <MasterTableView HierarchyDefaultExpanded="true" DataKeyNames="PurchaseDeliveryAssetID">
                        <Columns>
                            <telerik:GridBoundColumn HeaderText="Asset" DataField="AssetType" SortExpression="AssetType"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText='<%$ Resources:CommonTerms, Quantity %>' DataField="PurchaseDeliveryAssetQuantity" DataFormatString="{0:F0}" SortExpression="PurchaseOrderDetailQuantity"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText='<%$ Resources:CommonTerms, Price %>' DataField="AssetUnitPrice" DataFormatString="{0:F2}" SortExpression="ArticleUnitPrice"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText='<%$ Resources:CommonTerms, Currency %>' DataField="Currency" SortExpression="ArticleName"></telerik:GridBoundColumn>
                        </Columns>
                    </MasterTableView>
                </telerik:RadGrid>
            </asp:Panel>
 
        </NestedViewTemplate>
    </MasterTableView>
</telerik:RadGrid>

Code behind:
Private Sub FillPage(ByVal id As Int32)
 
    Dim order = Univeg.BL.PurchaseOrder.GetByID(id, SessionVars.CuCo)
 
    If order Is Nothing Then
        lblErrorPurchaseOrderNotFound.Visible = True
        Return
    End If
 
    Me.PurchaseOrderID = id
 
    lblNumber2.Text = order.PurchaseOrderNumber
    lblSupplierName.Text = order.SupplierCode & " - " & order.SupplierName
    lblSupplierCity.Text = order.SupplierZipCode & " - " & order.SupplierCity
 
    lblWarehouse.Text = order.WarehouseCode & " - " & order.WarehouseName
    lblOrderStatus.Text = order.PurchaseOrderStatusDescription
    lblDepartment.Text = order.DepartmentName
 
    Dim orderlines = Univeg.BL.PurchaseOrderDetail.GetLinesForOrder(id, SessionVars.CuCo)
    ' get delivery lines
    DeliveryLines = Univeg.BL.PurchaseDelivery.GetPurchaseLinesForOrder(id, SessionVars.CuCo)
    ' get delivery assets
    DeliveryAssetLines = Univeg.BL.PurchaseDeliveryAsset.GetListForOrder(id)
 
    RadGridOrderLines.DataSource = orderlines
    RadGridOrderLines.DataBind()
 
    Dim assets = Univeg.BL.PurchaseOrderAsset.GetListForOrder(id)
 
    lblAssetsCount.Text = assets.Count.ToString()
    RadGridAssets.DataSource = assets
    RadGridAssets.DataBind()
 
End Sub
 
Protected Sub RadGridDelivery_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs)
 
    Dim RadGridDelivery As Telerik.Web.UI.RadGrid = sender
    Dim nestedItem As GridNestedViewItem = DirectCast(TryCast(sender, RadGrid).NamingContainer, GridNestedViewItem)
    Dim PurchaseOrderDetailID As Int32 = nestedItem.ParentItem.GetDataKeyValue("PurchaseOrderDetailID")
 
    If DeliveryLines Is Nothing Then
        DeliveryLines = Univeg.BL.PurchaseDelivery.GetPurchaseLinesForOrder(Me.PurchaseOrderID, SessionVars.CuCo)
    End If
 
    If Not DeliveryLines Is Nothing Then
 
        Dim delivery = (From dl In DeliveryLines _
         Where dl.PurchaseOrderDetailID = PurchaseOrderDetailID _
         Order By dl.PurchaseDeliveryTHT _
         Select dl).ToList()
 
        If delivery.Count > 0 Then
            RadGridDelivery.DataSource = delivery
 
            Dim lblDeliveriesCount As Label = nestedItem.FindControl("lblDeliveriesCount")
            If Not lblDeliveriesCount Is Nothing Then
                lblDeliveriesCount.Text = delivery.Count.ToString()
            End If
        Else
            RadGridDelivery.Visible = False
            Dim panelDeliveries As Panel = nestedItem.FindControl("panelDeliveries")
            If Not panelDeliveries Is Nothing Then
                panelDeliveries.Visible = False
            End If
        End If
 
    End If
 
End Sub
 
Protected Sub RadGridAssets_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs)
 
    Dim RadGridAssets As Telerik.Web.UI.RadGrid = sender
    Dim nestedItem As GridNestedViewItem = DirectCast(TryCast(sender, RadGrid).NamingContainer, GridNestedViewItem)
    Dim PurchaseOrderDetailID As Int32 = nestedItem.ParentItem.GetDataKeyValue("PurchaseOrderDetailID")
 
    If Not DeliveryAssetLines Is Nothing Then
 
        Dim assets = (From da In DeliveryAssetLines _
         Where da.PurchaseOrderDetailID = PurchaseOrderDetailID _
         Order By da.AssetType _
         Select da).ToList()
 
        If assets.Count > 0 Then
            RadGridAssets.DataSource = assets
 
            Dim lblAssetsCount As Label = nestedItem.FindControl("lblAssetsCount")
            If Not lblAssetsCount Is Nothing Then
                lblAssetsCount.Text = assets.Count.ToString()
            End If
        Else
            Dim panelAssets As Panel = nestedItem.FindControl("panelAssets")
            If Not panelAssets Is Nothing Then
                panelAssets.Visible = False
            End If
        End If
 
    End If
 
End Sub
 
Protected Sub RadGridOrderLines_ItemCreated(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles RadGridOrderLines.ItemCreated
 
    If TypeOf e.Item Is GridNestedViewItem Then
 
        Dim RadGridDelivery As RadGrid = e.Item.FindControl("RadGridDelivery")
        Dim RadGridAssets As RadGrid = e.Item.FindControl("RadGridAssets")
 
        If Not RadGridDelivery Is Nothing Then
            AddHandler RadGridDelivery.NeedDataSource, AddressOf RadGridDelivery_NeedDataSource
        End If
 
        If Not RadGridAssets Is Nothing Then
            AddHandler RadGridAssets.NeedDataSource, AddressOf RadGridAssets_NeedDataSource
        End If
 
    End If
 
End Sub

2 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 23 Jan 2013, 05:09 AM
Hello,

Please try with below code snippet.

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource"
            AllowPaging="true" AllowFilteringByColumn="true" ShowFooter="true" AllowMultiRowSelection="true"
            AllowMultiRowEdit="true" PageSize="10" OnItemDataBound="RadGrid1_ItemDataBound"
            OnDetailTableDataBind="RadGrid1_DetailTableDataBind">
            <ExportSettings ExportOnlyData="true">
            </ExportSettings>
            <MasterTableView CommandItemDisplay="Top" EditMode="InPlace" Name="Parent" DataKeyNames="ID"  HierarchyDefaultExpanded="true">
                <Columns>
                    <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridEditCommandColumn>
                    </telerik:GridEditCommandColumn>
                </Columns>
                <NestedViewTemplate>
                    <asp:Panel CssClass="pad5" runat="server" ID="panelDeliveries">
                        <telerik:RadGrid ID="RadGridDelivery" AutoGenerateColumns="false" AllowPaging="false"
                            runat="server" OnNeedDataSource="RadGridDelivery_NeedDataSource">
                            <MasterTableView>
                                <Columns>
                                    <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                                    </telerik:GridBoundColumn>
                                </Columns>
                            </MasterTableView>
                        </telerik:RadGrid>
                    </asp:Panel>
                    <asp:Panel runat="server" ID="panelAssets" CssClass="pad5">
                        <telerik:RadGrid ID="RadGridAssets" AutoGenerateColumns="false" AllowPaging="false"
                            OnNeedDataSource="RadGridAssets_NeedDataSource" runat="server">
                            <MasterTableView HierarchyDefaultExpanded="true">
                                <Columns>
                                    <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                                    </telerik:GridBoundColumn>
                                </Columns>
                            </MasterTableView>
                        </telerik:RadGrid>
                    </asp:Panel>
                </NestedViewTemplate>
            </MasterTableView>
            <ClientSettings>
                <Selecting AllowRowSelect="True" />
            </ClientSettings>
            <PagerStyle AlwaysVisible="True" />
        </telerik:RadGrid>
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
       dynamic data = new[] {
           new { ID = 1, Name ="Name1",path="1.jpg",Customedate=DateTime.Now,ReceivedDate=DateTime.Now.AddDays(1)},
           new { ID = 2, Name = "Name2",path="2.jpg",Customedate=DateTime.Now,ReceivedDate=DateTime.Now.AddDays(2)},
           new { ID = 3, Name = "Name3",path="3.jpg",Customedate=DateTime.Now,ReceivedDate=DateTime.Now.AddDays(3)},
            new { ID = 4, Name = "Name4",path="2.jpg",Customedate=DateTime.Now,ReceivedDate=DateTime.Now.AddDays(4)},
           new { ID = 5, Name = "Name5",path="3.jpg",Customedate=DateTime.Now,ReceivedDate=DateTime.Now.AddDays(5)},
            new { ID = 6, Name ="Name1",path="1.jpg",Customedate=DateTime.Now,ReceivedDate=DateTime.Now.AddDays(6)},
           new { ID = 7, Name = "Name2",path="2.jpg",Customedate=DateTime.Now,ReceivedDate=DateTime.Now.AddDays(7)},
           new { ID = 8, Name = "Name3",path="3.jpg",Customedate=DateTime.Now,ReceivedDate=DateTime.Now.AddDays(8)},
            new { ID = 9, Name = "Name4",path="2.jpg",Customedate=DateTime.Now,ReceivedDate=DateTime.Now.AddDays(9)},
           new { ID = 10, Name = "Name5",path="3.jpg",Customedate=DateTime.Now,ReceivedDate=DateTime.Now.AddDays(10)},
            new { ID = 11, Name ="Name1",path="1.jpg",Customedate=DateTime.Now,ReceivedDate=DateTime.Now.AddDays(11)},
           new { ID = 12, Name = "Name2",path="2.jpg",Customedate=DateTime.Now,ReceivedDate=DateTime.Now.AddDays(12)},
           new { ID = 13, Name = "Name3",path="3.jpg",Customedate=DateTime.Now,ReceivedDate=DateTime.Now.AddDays(13)},
            new { ID = 14, Name = "Name4",path="2.jpg",Customedate=DateTime.Now,ReceivedDate=DateTime.Now.AddDays(14)},
           new { ID = 15, Name = "Name5",path="3.jpg",Customedate=DateTime.Now,ReceivedDate=DateTime.Now.AddDays(150)}
            
       };
 
       RadGrid1.DataSource = data;
 
   }
 
 
   protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = e.Item as GridDataItem;
 
       }
 
       if (e.Item is GridEditableItem && e.Item.IsInEditMode)
       {
           if (e.Item is GridDataInsertItem)
           {
               GridEditableItem item = e.Item as GridEditableItem;
 
           }
       }
 
 
   }
 
   protected void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
   {
 
   }
 
   protected void RadGridDelivery_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
       RadGrid r1 = sender as RadGrid;
       int ID = Convert.ToInt32(((r1.NamingContainer as GridNestedViewItem).ParentItem as GridDataItem).GetDataKeyValue("ID").ToString());
       dynamic data1 = new[] {
              new { ID = 1 + ID, Name ="Name_1"},
              new { ID = 2+ ID, Name = "Name_2"},
              new { ID = 3+ ID, Name = "Name_3"},
              new { ID = 4+ ID, Name = "Name_4"},
              new { ID = 5+ ID, Name = "Name_5"}
          };
 
 
       r1.DataSource = data1;
 
   }
   protected void RadGridAssets_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
 
       RadGrid r1 = sender as RadGrid;
       string ID = ((r1.NamingContainer as GridNestedViewItem).ParentItem as GridDataItem).GetDataKeyValue("ID").ToString();
       dynamic data1 = new[] {
              new { ID = 1, Name ="Name_1"+ ID},
              new { ID = 2, Name = "Name_2"+ ID},
              new { ID = 3, Name = "Name_3"+ ID},
              new { ID = 4, Name = "Name_4"+ ID},
              new { ID = 5, Name = "Name_5"+ ID}
          };
 
 
       r1.DataSource = data1;
   }





Thanks,
Jayesh Goyani
0
Koen
Top achievements
Rank 1
answered on 31 Jan 2013, 06:16 PM
Thanks a lot for your solution Jayesh
that worked!

Best regards,

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