How to refer to parent DataGrid from DataGridItem – use NamingContainer

It’s published in a few places, but I had to do a few Google searches before confirming that you should use NamingContainer property to get parent DataGrid for DataGridItem. So I’ve created a shared method in my WebFormsHelper class.

    Public Shared Function ParentDataGrid(ByVal Item As DataGridItem) As DataGrid

        ‘ Dim gridTable As DataGridTable = Item.Parent ‘DataGrid = gridTable.Parent doesn’t work because  MS declared DataGridTable as private(why?)

        Dim grid As DataGrid = Item.NamingContainer

        Return grid

    End Function