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

Get datakey for nestedview

3 Answers 168 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 28 Jul 2011, 07:35 PM

I am working int he databound event trying to get the datakey value of my grid to populate my nestedview.  I have a tope level grid then a subgrid and then a nestedview of this.  how can i get the datakey of the second level grid to populate the nestedview values. I have tried this but no joy give me an error
item.OwnerTableView.DataKeyValues(item.ItemIndex)("intPositionId").ToString

 Protected Sub myGridDeploy_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles myGridDeploy.ItemDataBound
        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, si.Email " _
                & "From  tblPersonnel as mp LEFT JOIN vw_PersonrInfo  as si on si.strSSN = mp.strSSN where intPositionId = " & item.OwnerTableView.DataKeyValues(item.ItemIndex)("intPositionId").ToString

            myDataTable = New DataTable
            myDataTable = getData(sql)

            TryCast(item.FindControl("lblPerson"), 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("lblEmail"), Label).Text = myDataTable.Rows(0)(14)

        End If
    End Sub

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 29 Jul 2011, 04:48 AM
Hello Kevin,

Try the following code snippet in ItemDataBound event to get the DataKeyValue.

VB:
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

Thanks,
Princy
0
Kevin
Top achievements
Rank 1
answered on 29 Jul 2011, 02:03 PM
Ok,

Thats a no go it gives me the following error.

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)

 

 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_Info  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

0
Pavlina
Telerik team
answered on 03 Aug 2011, 06:38 PM
Hello 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

Best wishes,
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
Princy
Top achievements
Rank 2
Kevin
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or