Xceed DataGrid for WPF v7.2 Documentation
GetContainerFromItem Method (DataGridControl)
Example 


Xceed.Wpf.DataGrid Assembly > Xceed.Wpf.DataGrid Namespace > DataGridControl Class : GetContainerFromItem Method
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.
Retrieves a container of the specified item.
Syntax
'Declaration
 
Public Function GetContainerFromItem( _
   ByVal item As Object _
) As DependencyObject
'Usage
 
Dim instance As DataGridControl
Dim item As Object
Dim value As DependencyObject
 
value = instance.GetContainerFromItem(item)
public DependencyObject GetContainerFromItem( 
   object 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.
Exceptions
ExceptionDescription
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.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;
  }
}
Requirements

Target Platforms: Windows 11, Windows, 10, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

DataGridControl Class
DataGridControl Members