DataGrid.SelectedCellsChanged Event

Occurs when the SelectedCells collection changes.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)

Public Event SelectedCellsChanged As SelectedCellsChangedEventHandler
<object SelectedCellsChanged="SelectedCellsChangedEventHandler" .../>

You can handle the SelectedCellsChanged event to be notified when the collection of selected cells is changed. If the selection includes full rows, the Selector.SelectionChanged event is also raised.

You can retrieve the AddedCells and RemovedCells from the SelectedCellsChangedEventArgs in the event handler.

The following example shows how to handle the SelectedCellsChanged event and clear the values in the newly selected cells.

    <DataGrid Name="DG1" ItemsSource="{Binding}" SelectionUnit="CellOrRowHeader" SelectionChanged="DG1_SelectionChanged" SelectedCellsChanged="DG1_SelectedCellsChanged" />
Private Sub DG1_SelectedCellsChanged(ByVal sender As Object, ByVal e As SelectedCellsChangedEventArgs)
    'Get the newly selected cells 
    Dim selectedcells As IList(Of DataGridCellInfo) = e.AddedCells

    'Get the value of each newly selected cell 
    For Each di As DataGridCellInfo In selectedcells
        'Cast the DataGridCellInfo.Item to the source object type 
        'In this case the ItemsSource is a DataTable and individual items are DataRows 
        Dim dvr As DataRowView = DirectCast(di.Item, DataRowView)

        'Clear values for all newly selected cells 
        Dim cr As AdventureWorksLT2008DataSet.CustomerRow = DirectCast(dvr.Row, AdventureWorksLT2008DataSet.CustomerRow)
        cr.SetField(di.Column.DisplayIndex, "")

End Sub

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft