SpreadsheetControl.Selection Property

Gets or sets a cell range selected in the active worksheet.

Namespace: DevExpress.XtraSpreadsheet

Assembly: DevExpress.XtraSpreadsheet.v20.1.dll

Declaration

[Browsable(false)]
public CellRange Selection { get; set; }
<Browsable(False)>
Public Property Selection As CellRange

Property Value

Type Description
CellRange

A CellRange object specifying the cell selection in a worksheet that is currently active. If you assign a cell range located in a worksheet that is not active, an exception is raised.

Remarks

The Selection property specifies the range of cells selected in a worksheet that is currently active (B3:F9, in the image below). The SpreadsheetControl.SelectedCell property specifies a cell located in the active worksheet where data is inserted when an end-user types (E7, in the image below). In other words, this is an active cell ( i.e., a cell that is in focus) that can also be obtained via the SpreadsheetControl.ActiveCell property. The Selection can be a single cell, a contiguous or noncontiguous (union) range of cells, while the SelectedCell is always a single cell inside the current selection. If you set SelectedCell to a range that contains more than one cell, the top left cell of this range becomes the active cell.

SpreadsheetControl_Worksheet_Selection

The following code sets the selection and active cell as shown in the image above.


using DevExpress.Spreadsheet;
// ...

IWorkbook workbook = spreadsheetControl1.Document;
Worksheet activeSheet = workbook.Worksheets.ActiveWorksheet;

// Specify the selected cell range in the active worksheet. 
spreadsheetControl1.Selection = activeSheet.Range["B3:F9"];
// Specify the active cell in the active worksheet. 
spreadsheetControl1.SelectedCell = activeSheet.Cells["E7"];

If the cell you assign to SelectedCell belongs to the currently selected range, the selection will not be changed and the specified cell will become active within this selection. For example, executing the following code results in the selection and active cell shown in the image below.


spreadsheetControl1.Selection = activeSheet.Range["B3:D6"];
spreadsheetControl1.SelectedCell = activeSheet.Range["C5:E7"];

SpreadsheetControl_Worksheet_Selection_Example1

If the cell you activate is outside the currently selected range, the selection will be changed - it will coincide with the specified active cell. See the example below.


spreadsheetControl1.Selection = activeSheet.Range["B3:D6"];
spreadsheetControl1.SelectedCell = activeSheet.Range["A2:C2"];

SpreadsheetControl_Worksheet_Selection_Example2

You can also select multiple non-adjacent cells or cell ranges in the worksheet simultaneously. If there is more than one selected range in the worksheet (A2:C4, C6:C9, E3:F7, in the image below), the Selection property returns a complex (union) range that includes all the selected areas. You can access an individual range in the selection by its index in the CellRange.Areas collection.

You can also use the SpreadsheetControl.SetSelectedRanges and SpreadsheetControl.GetSelectedRanges methods to set or obtain a collection of selected ranges in the worksheet.

Spreadsheet_Worksheet_MultipleSelectedRanges_Example2

NOTE

To specify the cell selection and active cell in a specific worksheet of the document loaded in the SpreadsheetControl, use the Worksheet.Selection, Worksheet.SelectedCell, Worksheet.SetSelectedRanges and Worksheet.GetSelectedRanges members of the corresponding worksheet object.

Implements

DevExpress.XtraSpreadsheet.ISpreadsheetControl.Selection
See Also