Back to Devexpress

SpreadsheetControl.SelectionChanged Event

wpf-devexpress-dot-xpf-dot-spreadsheet-dot-spreadsheetcontrol-4cda4308.md

latest6.9 KB
Original Source

SpreadsheetControl.SelectionChanged Event

Fires when the selection changes in an active worksheet.

Namespace : DevExpress.Xpf.Spreadsheet

Assembly : DevExpress.Xpf.Spreadsheet.v25.2.dll

NuGet Package : DevExpress.Wpf.Spreadsheet

Declaration

csharp
public event EventHandler SelectionChanged
vb
Public Event SelectionChanged As EventHandler

Event Data

The SelectionChanged event's data class is EventArgs.

Remarks

Handle the SelectionChanged event to perform any actions each time a user selects cells, rows, columns, or drawing objects in the SpreadsheetControl’s UI.

Set the SpreadsheetControlOptions.RaiseEventsOnModificationsViaAPI property to true to raise the SelectionChanged event when the selection is changed in code.

Use the following API members to specify the selection:

Example

The example below shows how to use the SelectionChanged event to calculate the average, count, numerical count and sum for non-empty selected cells.

csharp
spreadsheetControl1.SelectionChanged += (s, e) =>
{
    int count = 0;
    double sum = 0.0;
    int numericCount = 0;
    double average = 0.0;

    Worksheet worksheet = spreadsheetControl1.ActiveWorksheet;
    Range selectedCells = worksheet.Selection.Intersect(worksheet.GetDataRange());
    if (selectedCells != null)
    {
        foreach (Cell cell in selectedCells.ExistingCells)
        {
            count++;
            if (cell.Value.IsNumeric)
            {
                numericCount++;
                sum += cell.Value.NumericValue;
            }
        }
    }
    if (numericCount > 0)
        average = sum / numericCount;
};
vb
AddHandler spreadsheetControl1.SelectionChanged,
    Sub(s, e)
        Dim count As Integer = 0
        Dim sum As Double = 0.0
        Dim numericCount As Integer = 0
        Dim average As Double = 0.0

        Dim worksheet As Worksheet = spreadsheetControl1.ActiveWorksheet
        Dim selectedCells As Range = worksheet.Selection.Intersect(worksheet.GetDataRange())
        If selectedCells IsNot Nothing Then
            For Each cell As Cell In selectedCells.ExistingCells
                count += 1
                If cell.Value.IsNumeric Then
                    numericCount += 1
                    sum += cell.Value.NumericValue
                End If
            Next cell
        End If
        If numericCount > 0 Then
            average = sum / numericCount
        End If
    End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the SelectionChanged event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

wpf-spreadsheet-control-create-a-data-entry-form/CS/WpfDataEntryFormSample/MainWindow.xaml#L30

xml
ProtectionWarning="SpreadsheetControl1_ProtectionWarning"
SelectionChanged="SpreadsheetControl1_SelectionChanged">
<dxsps:SpreadsheetControl.Options>

wpf-spreadsheet-control-create-a-data-entry-form/CS/WpfDataEntryFormSample/obj/Debug/net8.0-windows/MainWindow.g.cs#L118

csharp
#line 30 "..\..\..\MainWindow.xaml"
this.spreadsheetControl1.SelectionChanged += new System.EventHandler(this.SpreadsheetControl1_SelectionChanged);

wpf-spreadsheet-control-create-a-data-entry-form/VB/WpfDataEntryFormSample/obj/Debug/net8.0-windows/MainWindow.g.vb#L116

vb
#ExternalSource("..\..\..\MainWindow.xaml",30)
AddHandler Me.spreadsheetControl1.SelectionChanged, New System.EventHandler(AddressOf Me.SpreadsheetControl1_SelectionChanged)

See Also

SpreadsheetControl Class

SpreadsheetControl Members

DevExpress.Xpf.Spreadsheet Namespace