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

get nested view datakey

5 Answers 172 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 02 Aug 2011, 02:28 PM

What I have is a grid that contains a subgrid and then a nested view in the subgrid.  I am trying to get a hold of the datakey of the subgrid to populate the nestedview automatically,  Right now I have a static Id in the code to make sure it works but need to get the dynamic Id.  This was given to me before but did not work.  I got an error with it.  how can I get a hold of the datakey for the sub grid to populate the nested view of the subgrid.

Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim innergrid As RadGrid = DirectCast(sender, RadGrid)
        Dim nesteditem As GridNestedViewItem = DirectCast(innergrid.NamingContainer, GridNestedViewItem)
        Dim parentItem As GridDataItem = DirectCast(nesteditem.ParentItem, GridDataItem)
        Dim Id As String = parentItem.GetDataKeyValue("PositionId").ToString()
    End If
End Sub

Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'Telerik.Web.UI.GridNestedViewItem'.

 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'Telerik.Web.UI.GridNestedViewItem'.

Source Error:

Line 48:         If TypeOf e.Item Is GridDataItem Then
Line 49:             Dim innergrid As RadGrid = DirectCast(sender, RadGrid)
Line 50: Dim nesteditem As GridNestedViewItem = DirectCast(innergrid.NamingContainer








<telerik:RadGrid ID="myGridDeploy" runat="server" Width="95%" BorderWidth="1px" CellPadding="6" GridLines="None" BorderColor="#404040" Skin="Web20">
                    <MasterTableView AutoGenerateColumns="false" DataKeyNames="intUnitMobId" HierarchyDefaultExpanded="false" HierarchyLoadMode="ServerBind"
                        BorderColor="#404040" Font-Size="12" Font-Names="Veranda,arial,sans-serif" HeaderStyle-HorizontalAlign="Center"
                        GridLines="Both" BorderWidth="1px" ExpandCollapseColumn-ButtonType="ImageButton" ExpandCollapseColumn-CollapseImageUrl="~/Images/30.png"
                        ExpandCollapseColumn-ExpandImageUrl="~/Images/29.png"><ItemStyle HorizontalAlign="Center" />
                        <AlternatingItemStyle BackColor="#B0C4DE"  HorizontalAlign="Center" />
                        <HeaderStyle ForeColor="White" Font-Bold="true" BorderColor="#404040" BorderWidth="1px" />
                            <DetailTables>
                                 <telerik:GridTableView DataKeyNames="intPositionId" Name="myUnitPos" Width="100%" TableLayout="Fixed" BorderWidth="1px" CellPadding="6" Font-Size="10"
                                    AutoGenerateColumns="False" HeaderStyle-HorizontalAlign="Center" BorderColor="#404040" Font-Names="Veranda,arial,sans-serif" GridLines="Both" ExpandCollapseColumn-ButtonType="ImageButton"
                                    ExpandCollapseColumn-CollapseImageUrl="~/Images/30.png" ExpandCollapseColumn-ExpandImageUrl="~/Images/29.png">
                                    <ParentTableRelation>
                                        <telerik:GridRelationFields DetailKeyField="intUnitMobId" MasterKeyField="intUnitMobId" />
                                    </ParentTableRelation>
                                    <HeaderStyle Font-Bold="true" HorizontalAlign="Center" CssClass="InnerSubHeaderStyle" />
                                    <ItemStyle CssClass="InnerSubItemStyle" HorizontalAlign="Center"  />
                                    <AlternatingItemStyle CssClass="InnerSubAlernatingItemStyle" HorizontalAlign="Center" />
                                        <NestedViewSettings>
                                            <ParentTableRelation>
                                                 <telerik:GridRelationFields DetailKeyField="intPositionId" MasterKeyField="intPositionId" />
                                            </ParentTableRelation>
                                        </NestedViewSettings>
                                        <NestedViewTemplate>
                                            <asp:Panel ID="pnlInfo" runat="server" BorderStyle="Double" BorderColor="#85A3E0" Width="90%">
                                                <table >
                                                    <tr>
                                                        <td><u>Soldier Information</u></td>
                                                    </tr>
                                                    <tr>
                                                        <td style="height:5px"></td>
                                                    </tr>
                                                     <tr>
                                                        <td>
                                                            <b>Name:</b>&nbsp;<asp:Label ID="lblPersonnel" runat="server"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
                                                            <b>Rank:</b>&nbsp;<asp:label ID="lblRank" runat="server"></asp:label>&nbsp;&nbsp;&nbsp;&nbsp;
                                                            <b>Age:</b>&nbsp;<asp:Label ID="lblAge" runat="server"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
                                                            <b>PMOS:</b>&nbsp;<asp:Label ID="lblPMOS" runat="server"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
                                                            <b>SMOS:</b>&nbsp;<asp:label ID="lblSMOS" runat="server"></asp:label>&nbsp;&nbsp;&nbsp;&nbsp;
                                                            <b>POSN Excess:</b>&nbsp;<asp:Label ID="lblPOSN" runat="server"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
                                                            <b>Clearance:</b>&nbsp;<asp:Label ID="lblClear" runat="server"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
                                                            <b>ETS\MRD:</b>&nbsp;<asp:label ID="lblEts" runat="server"></asp:label>
                                                         </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="height:5px"></td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <b>Full-Time:</b>&nbsp;<asp:Label ID="lblFullTime" runat="server"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
                                                            <b>PULHES:</b>&nbsp;<asp:Label ID="lblPuhles" runat="server"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
                                                            <b>Yrs Active:</b>&nbsp;<asp:label ID="lblYrsActive" runat="server"></asp:label>&nbsp;&nbsp;&nbsp;&nbsp;
                                                            <b>SRP Date:</b>&nbsp;<asp:Label ID="lblDtSrp" runat="server"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
                                                            <b>Deployable:</b>&nbsp;<asp:label ID="lblDeplyable" runat="server"></asp:label>&nbsp;&nbsp;&nbsp;&nbsp;
                                                            <b>DMOSQ:</b>&nbsp;<asp:label ID="lblDMOSQ" runat="server"></asp:label>&nbsp;&nbsp;&nbsp;&nbsp;
                                                            <b>Email:</b>&nbsp;<asp:Label ID="lblEmail" runat="server"></asp:Label>
                                                        </td>
                                                    </tr>
                                                </table>
                                            </asp:Panel>
                                        </NestedViewTemplate>
                                        <Columns>
                                            <telerik:GridBoundColumn DataField="strPosnTitle" HeaderText="Title" />
                                            <telerik:GridBoundColumn DataField="strPara" HeaderText="Para" />
                                            <telerik:GridBoundColumn DataField="strLine" Headertext="Line" />
                                            <telerik:GridBoundColumn DataField="intPositionNum" HeaderText="Position" />
                                            <telerik:GridBoundColumn DataField="strGrade" HeaderText="Grade" />
                                            <telerik:GridBoundColumn DataField="strMos" HeaderText="Mos" />
                                            <telerik:GridBoundColumn DataField="strASI" HeaderText="ASI" />
                                            <telerik:GridBoundColumn DataField="Filled" HeaderText="Filled" />
                                            <telerik:GridTemplateColumn HeaderText="Add">
                                                <ItemTemplate>
                                                    <asp:ImageButton runat="server" ID="imgAdd"  CommandArgument='<%# bind("intPositionId") %>' CommandName="AddSoldier" ImageUrl="~/Images/29.png" />
                                                </ItemTemplate>
                                            </telerik:GridTemplateColumn>
                                             <telerik:GridTemplateColumn HeaderText="Add">
                                                <ItemTemplate>
                                                    <asp:ImageButton runat="server" ID="imgEdit"  CommandArgument='<%# bind("intPositionId") %>' CommandName="EditSoldier" ImageUrl="~/Images/Edit_icon.png" />
                                                </ItemTemplate>
                                            </telerik:GridTemplateColumn>
                                        </Columns>
                                 </telerik:GridTableView>
                             </DetailTables>
                            <Columns>
                                <telerik:GridBoundColumn DataField="strUIC" HeaderText="UIC" />
                                <telerik:GridBoundColumn DataField="strDeployment" HeaderText="Deployment" />
                                <telerik:GridBoundColumn DataField="strAttchUic" HeaderText="BN UIC" />
                                <telerik:GridBoundColumn DataField="dtDeploy" Headertext="DT_Deploy" />
                                <telerik:GridBoundColumn DataField="dtDemob" HeaderText="DT_Demob" />
                                <telerik:GridTemplateColumn HeaderText="Import Soldiers">
                                    <ItemTemplate>
                                            <asp:ImageButton runat="server" ID="imgAddPosition"  CommandArgument='<%# bind("intUnitMobId") %>' CommandName="AddMul" ImageUrl="~/Images/29.png" />
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridBoundColumn DataField="Assign" HeaderText="Assigned" />
                                <telerik:GridBoundColumn DataField="nonAssign" HeaderText="Fill" />
                                <telerik:GridBoundColumn DataField="Total" HeaderText="Total" />
                            </Columns>
                    </MasterTableView>
                </telerik:RadGrid>

 

 

Protected Sub myGridDeploy_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles myGridDeploy.ItemDataBound

        If TypeOf e.Item Is GridDataItem Then
            Dim innergrid As RadGrid = DirectCast(sender, RadGrid)
            Dim nesteditem As GridNestedViewItem = DirectCast(innergrid.NamingContainer, GridNestedViewItem)
            Dim parentItem As GridDataItem = DirectCast(nesteditem.ParentItem, GridDataItem)
            Dim Id As String = parentItem.GetDataKeyValue("PositionId").ToString()
        End If

        If TypeOf e.Item Is GridNestedViewItem Then
            Dim item As GridNestedViewItem = DirectCast(e.Item, GridNestedViewItem)
          
            sql = "Select mp.strSSN, si.FullName, si.strRank, si.Age, si.strPmos, si.strSmos, si.POSN_NBR_EXCESS_IND, si.SCTY_CLNC, si.ETS, si.strStatus, si.PHYS_PRFL_SER, si.intYearSvc, si.SRPDate, Deployable, " _
                & "Case when mp.intDMOSQ = 1 then 'YES' ELSE 'NO' END intDMOSQ, si.Email " _
                & "From  tblMobUnitPersonnel as mp LEFT JOIN vw_SoldierInfo  as si on si.strSSN = mp.strSSN where intPositionId = " & Id

            myDataTable = New DataTable
            myDataTable = getData(sql)

            TryCast(item.FindControl("lblPersonnel"), Label).Text = myDataTable.Rows(0)(1)
            TryCast(item.FindControl("lblRank"), Label).Text = myDataTable.Rows(0)(2)
            TryCast(item.FindControl("lblAge"), Label).Text = myDataTable.Rows(0)(3)
            TryCast(item.FindControl("lblPmos"), Label).Text = myDataTable.Rows(0)(4)
            If IsDBNull(myDataTable.Rows(0)(5)) Then
                myDataTable.Rows(0)(5) = "None"
            End If
            TryCast(item.FindControl("lblSmos"), Label).Text = myDataTable.Rows(0)(5)
            TryCast(item.FindControl("lblPosn"), Label).Text = myDataTable.Rows(0)(6)
            TryCast(item.FindControl("lblClear"), Label).Text = myDataTable.Rows(0)(7)
            TryCast(item.FindControl("lblEts"), Label).Text = myDataTable.Rows(0)(8)
            TryCast(item.FindControl("lblFullTime"), Label).Text = myDataTable.Rows(0)(9)
            TryCast(item.FindControl("lblPuhles"), Label).Text = myDataTable.Rows(0)(10)
            TryCast(item.FindControl("lblYrsActive"), Label).Text = myDataTable.Rows(0)(11)
            If IsDBNull(myDataTable.Rows(0)(12)) Then
                myDataTable.Rows(0)(12) = "None"
            End If
            TryCast(item.FindControl("lblDtSrp"), Label).Text = myDataTable.Rows(0)(12)
            TryCast(item.FindControl("lblDeplyable"), Label).Text = myDataTable.Rows(0)(13)
            TryCast(item.FindControl("lblDMOSQ"), Label).Text = myDataTable.Rows(0)(14)
            TryCast(item.FindControl("lblEmail"), Label).Text = myDataTable.Rows(0)(15)

        End If
    End Sub

5 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Aug 2011, 06:31 PM
Hello,

Your code :Dim parentItem As GridDataItem = DirectCast(nesteditem.ParentItem, GridDataItem)
Solution : Dim parentItem As GridDataItem = DirectCast(nesteditem.ParentItem.Parent..........etc, GridDataItem)

Add Parent again and again still you not get the GridDataItem form this code.

Thanks,
Jayesh Goyani
0
Kevin
Top achievements
Rank 1
answered on 02 Aug 2011, 06:54 PM
Ok, I changed but now I get a new error out of it.
 If TypeOf e.Item Is GridDataItem Then
            Dim innergrid As RadGrid = DirectCast(sender, RadGrid)
            Dim nesteditem As GridNestedViewItem = DirectCast(innergrid.NamingContainer, GridNestedViewItem)
            Dim parentItem As GridDataItem = DirectCast(nesteditem.ParentItem.Parent, GridDataItem)
            Dim Id As String = parentItem.GetDataKeyValue("PositionId").ToString()
        End If


Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'Telerik.Web.UI.GridNestedViewItem'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'Telerik.Web.UI.GridNestedViewItem'.

Source Error:

Line 48:         If TypeOf e.Item Is GridDataItem Then
Line 49:             Dim innergrid As RadGrid = DirectCast(sender, RadGrid)
Line 50: Dim nesteditem As GridNestedViewItem = DirectCast(innergrid.NamingContainer, GridNestedViewItem)Line 51:             Dim parentItem As GridDataItem = DirectCast(nesteditem.ParentItem.Parent, GridDataItem)
Line 52:             Dim Id As String = parentItem.GetDataKeyValue("PositionId").ToString()

Source File: S:\inetpub\wwwroot\ReadinessTracker\Mobilization\J1\ManageSoldiers.aspx.vb    Line: 50

Stack Trace:

0
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Aug 2011, 07:05 PM
Hello,

means you have to take nesteditem.Parent.Parent.Parent.Parent......(add again and again "parent"  : still You not get griddataitem or still you not get the error.)

For Example :
 {
   RadGrid InnerGrid= (RadGrid)sender;
int ID = Convert.ToInt32(((GridNestedViewItem)InnerGrid.Parent.Parent.Parent.Parent.Parent).ParentItem.GetDataKeyValue("ID"));            }     

How many times we have to add parent its depend on our Html code in which we take how many container tag(panel).


Thanks,
Jayesh Goyani
0
Kevin
Top achievements
Rank 1
answered on 02 Aug 2011, 07:44 PM
HI.

All's I get when adding in more parents is more errors.  If I start on this line add add out parents
 Dim nesteditem As GridNestedViewItem = DirectCast(innergrid.parent.parent.NamingContainer, GridNestedViewItem)

If run into a error this error with any multiple of parents.
Unable to cast object of type 'ASP.site_master' to type 'Telerik.Web.UI.GridNestedViewItem'.

when I add to this line it gives me my original error with the line above.
 Dim parentItem As GridDataItem = DirectCast(nesteditem.ParentItem.Parent.Parent, GridDataItem)

When I add to both lines equally it gets me the error above always or my first initial error.


 If TypeOf e.Item Is GridDataItem Then
            Dim innergrid As RadGrid = DirectCast(sender, RadGrid)
            Dim nesteditem As GridNestedViewItem = DirectCast(innergrid.NamingContainer, GridNestedViewItem)
            Dim parentItem As GridDataItem = DirectCast(nesteditem.ParentItem.Parent.Parent, GridDataItem)
            Dim Id As String = parentItem.GetDataKeyValue("PositionId").ToString()
        End If
0
Pavlina
Telerik team
answered on 03 Aug 2011, 06:37 PM
Hi Kevin,

Please refer to the forum thread below which elaborates on similar subject and let me know if it helps to resolve the problem you are facing:
http://www.telerik.com/community/forums/aspnet-ajax/grid/radgrid-inside-nestedviewtemplate-of-another-dynamic-radgrid.aspx

Regards,
Pavlina
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Kevin
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or