SelectedItems Property
See Also  Example
Xceed.Wpf.DataGrid Assembly > Xceed.Wpf.DataGrid Namespace > DataGridControl Class : SelectedItems Property

Gets a collection of the data items that are currently selected in the grid.

Syntax

Visual Basic (Declaration) 
Public ReadOnly Property SelectedItems As IList
C# 
public IList SelectedItems {get;}

Return Value

A reference to a collection containing the data items currently selected in the grid. Can be a null reference (Nothing in Visual Basic) if items other than master data items are selected.

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 delete the selected items from the underlying data view when the delete key is pressed via the PreviewKeyDown event.
XAMLCopy Code
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"> 
  <Grid.Resources> 
    <xcdg:DataGridCollectionViewSource x:Key="cvs_orders" 
                                    Source="{Binding Source={x:Static Application.Current}, 
                                                      Path=Orders}"/>        
         
  </Grid.Resources> 
 
 <xcdg:DataGridControl x:Name="OrdersGrid" 
                       ItemsSource="{Binding Source={StaticResource cvs_orders}}" 
                       PreviewKeyDown="DeleteSelectedRows"> 
 </xcdg:DataGridControl> 
</Grid>
Visual BasicCopy Code
Private Sub DeleteSelectedRows(ByVal sender As Object, ByVal e As KeyEventArgs)
  Dim grid As DataGridControl = TryCast( sender, DataGridControl )
  If Not grid Is Nothing Then
    If e.Key = Key.Delete Then
      If grid.SelectedItems.Count > 0 Then
        ' we keep a copy of the selected items because once the first item is deleted,
        ' the selected items are reset to 0.
        Dim items As ArrayList = New ArrayList( grid.SelectedItems )
        Dim dataView As DataView = TryCast( ( CType( grid.ItemsSource, _
                       DataGridCollectionView ) ).SourceCollection, DataView )
        For i As Integer = items.Count - 1 To 0 Step -1
          dataView.Table.Rows.Remove( CType( items( i ), System.Data.DataRow ) )
        Next i
        grid.CurrentItem = grid.Items( 0 )
      End If
    End If
  End If
End Sub
C#Copy Code
Private void DeleteSelectedRows( Object sender, KeyEventArgs e )
{
DataGridControl grid = sender As DataGridControl;      
If( grid !=
null )
{
  If( e.Key == Key.Delete )
  {
    If( grid.SelectedItems.Count > 0 )
    {
      
// we keep a copy of the selected items because once the first item Is deleted,
      
// the selected items are reset To 0 And the source may be automatically modified To
      
// reflect the New items.
      
ArrayList items = New ArrayList( grid.SelectedItems );
      DataView dataView = ( ( DataGridCollectionView )grid.ItemsSource ).SourceCollection As DataView;
      For(
int i = items.Count - 1; i >= 0; i-- )
      {
        dataView.Table.Rows.Remove( ( System.Data.DataRow )items[ i ] );
      }
      grid.CurrentItem = grid.Items[ 0 ];
    }
  }
}
}
The SelectedItems property is also a dependency property; however, using a value-changed handler will not function in this case since the values of the collection are modified and not the collection itself. Although the SelectedItems property is exposed as an IList it is actually an ObservableCollection; therefore, in order to be notified when its content (i.e., the items that are selected) changes, it can be cast as an INotifyCollectionChanged and subscribe to its CollectionChanged event.
Visual BasicCopy Code
AddHandler CType( Me.EmployeesGrid.SelectedItems, INotifyCollectionChanged ).CollectionChanged, AddressOf Me.Window1_CollectionChanged
The SelectedItems property is also a dependency property; however, using a value-changed handler will not function in this case since the values of the collection are modified and not the collection itself. Although the SelectedItems property is exposed as an IList it is actually an ObservableCollection; therefore, in order to be notified when its content (i.e., the items that are selected) changes, it can be cast as an INotifyCollectionChanged and subscribe to its CollectionChanged event.
C#Copy Code
( ( INotifyCollectionChanged )this.EmployeesGrid.SelectedItems ).CollectionChanged += new NotifyCollectionChangedEventHandler( Window1_CollectionChanged );

Remarks

The SelectedItems, SelectedItem, and SelectedIndex properties represent data items and not DataRows.

In the case where more than 1 data item is selected in a grid, the SelectedItem property will return the first data item in the SelectedItems collection and the SelectedIndex property will return the index of this same item.

The CurrentItem and SelectedItem/SelectedItems properties must be set to a null reference (Nothing in Visual Basic) in order to reset them when the NavigationBehavior property is set to None.

Requirements

Supported Frameworks: Microsoft .NET Framework version 3.5

See Also