Retrieves a container of the specified item.
Syntax
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.
Exceptions
| Exception | Description |
| System.InvalidOperationException | The specified item is neither a master data item nor an item located in the headers or footers of a grid or master group. |
Example
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.
| Visual Basic | Copy Code |
|---|
Public Sub New()
InitializeComponent()
Dim grid As New DataGridControl()
Dim view As New DataGridCollectionView( App.Orders.DefaultView )
grid.ItemsSource = view
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
Dim grid As DataGridControl = CType( sender, DataGridControl )
Dim row As DataRow = TryCast( grid.GetContainerFromItem( grid.CurrentItem ), DataRow )
row.Background = Brushes.Pink
End If
End Sub |
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.
| Visual Basic | Copy Code |
|---|
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;
}
} |
Requirements
Supported Frameworks: Microsoft .NET Framework version 3.5
See Also