DataGrid.SelectedCellsChanged Event
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
Occurs when the SelectedCells collection changes.
Namespace: System.Windows.Controls
Assembly: PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
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.
<Grid> <DataGrid Name="DG1" ItemsSource="{Binding}" SelectionUnit="CellOrRowHeader" SelectionChanged="DG1_SelectionChanged" SelectedCellsChanged="DG1_SelectedCellsChanged" /> </Grid>
private void DG1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) { //Get the newly selected cells IList<DataGridCellInfo> selectedcells = e.AddedCells; //Get the value of each newly selected cell foreach (DataGridCellInfo di in selectedcells) { //Cast the DataGridCellInfo.Item to the source object type //In this case the ItemsSource is a DataTable and individual items are DataRows DataRowView dvr = (DataRowView)di.Item; //Clear values for all newly selected cells AdventureWorksLT2008DataSet.CustomerRow cr = (AdventureWorksLT2008DataSet.CustomerRow)dvr.Row; cr.BeginEdit(); cr.SetField(di.Column.DisplayIndex, ""); cr.EndEdit(); } }
Windows 8 Consumer Preview, Windows Server 8 Beta, Windows 7, Windows Server 2008 SP2, 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.