GetContainerFromItem Method (DataGridControl)
Retrieves a container of the specified item.
Parameters
- item
- The item whose container to retrieve. The item can correspond to a master data item or an item in the headers or footers of the grid or master group. Items located in the fixed headers or footers of the grid or in a detail will return throw an exception.
Return Value
A DependencyObject representing the container of the specified master item. Can be a null reference (Nothing in Visual Basic) if the item belongs to the context but does not have a container.
All examples in this topic assume that the grid is bound to the Orders table of the Northwind database, unless stated otherwise.
The following example demonstrates how to retrieve a DataRow (container) for a data item using the GetContainerFromItem method. It is important to remember that data rows are virtualized; therefore, references to them or their cells should never be kept.The following example demonstrates how to retrieve a DataRow (container) for a data item using the GetContainerFromItem method. It is important to remember that data rows are virtualized; therefore, references to them or their cells should never be kept.
Public Sub New()
InitializeComponent()
Dim grid As New DataGridControl()
Dim view As New DataGridCollectionView( App.Orders.DefaultView )
grid.ItemsSource = view
' Subscribe to the PropertyChanged event to know when the CurrentItem property changes.
AddHandler grid.PropertyChanged, AddressOf grid_PropertyChanged
Me.Content = grid
End Sub
Private Sub grid_PropertyChanged( ByVal sender As Object, _
ByVal e As System.ComponentModel.PropertyChangedEventArgs )
If e.PropertyName = "CurrentItem" Then
' Retrieve the data-row container for the current item. Can be Nothing if the data item
' is not in the viewport.
Dim grid As DataGridControl = CType( sender, DataGridControl )
Dim row As DataRow = TryCast( grid.GetContainerFromItem( grid.CurrentItem ), DataRow )
' Change the background of the data row to pink. Data rows are recycled once they
' exit the viewport; therefore, any modifications made to a data row will
' be discarded once it is no longer in the viewport.
row.Background = Brushes.Pink
End If
End Sub
Public Window1()
{
InitializeComponent();
DataGridControl grid = New DataGridControl();
DataGridCollectionView view = New DataGridCollectionView( App.Orders.DefaultView );
grid.ItemsSource = view;
// Subscribe To the PropertyChanged Event To know When the CurrentItem Property changes.
grid.PropertyChanged +=
New System.ComponentModel.PropertyChangedEventHandler( grid_PropertyChanged );
this.Content = grid;
}
void grid_PropertyChanged( Object sender, System.ComponentModel.PropertyChangedEventArgs e )
{
If( e.PropertyName == "CurrentItem" )
{
// Retrieve the Data-row container For the current item. Can be null If the Data item
// Is Not In the viewport.
DataGridControl grid = sender As DataGridControl;
DataRow row = grid.GetContainerFromItem( grid.CurrentItem ) As DataRow;
// Change the background of the Data row To pink. Data rows are recycled once they
// Exit the viewport; therefore, any modifications made To a Data row will
// be discarded once it Is no longer In the viewport.
row.Background = Brushes.Pink;
}
}
.NET: net5.0, net5.0-windows, net6.0, net6.0-macos, net6.0-windows, net7.0, net7.0-macos, net7.0-windows, net8.0, net8.0-browser, net8.0-macos, net8.0-windows, net9.0, net9.0-browser, net9.0-macos, net9.0-windows, net10.0, net10.0-browser, net10.0-macos, net10.0-windows.
.NET Framework: net40, net403, net45, net451, net452, net46, net461, net462, net463, net47, net471, net472, net48, net481.