Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

SpreadsheetControl.SelectionChanged Event

Fires when the selection changes in an active worksheet.

Namespace: DevExpress.XtraSpreadsheet

Assembly: DevExpress.XtraSpreadsheet.v24.2.dll

NuGet Package: DevExpress.Win.Spreadsheet

#Declaration

public event EventHandler SelectionChanged

#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 WorkbookEventOptions.RaiseOnModificationsViaAPI property to true to raise the SelectionChanged event when the selection is changed in code.

spreadsheetControl.Options.Events.RaiseOnModificationsViaAPI = true;

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.

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;
};
See Also