Worksheet.Selection Property
Gets or sets a cell range selected in the worksheet.
Namespace: DevExpress.Spreadsheet
Assembly: DevExpress.Spreadsheet.v24.2.Core.dll
NuGet Package: DevExpress.Spreadsheet.Core
Declaration
Property Value
Type | Description |
---|---|
CellRange | A CellRange object specifying the cell selection in the worksheet. If you assign a cell range located in a worksheet other than the current worksheet, an exception is raised. |
Remarks
The Selection property specifies the range of cells selected in the worksheet (B3:F9, in the image below). The Worksheet.SelectedCell property specifies a single cell where data is inserted when an end-user types (E7, in the image below). In other words, this is the active cell. 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, only this range’s top left cell is considered.
The following code sets the selection and active cell as shown in the image above.
using DevExpress.Spreadsheet;
// ...
IWorkbook workbook = spreadsheetControl1.Document;
Worksheet worksheet = workbook.Worksheets["Sheet1"];
// Specify the selected range of cells.
worksheet.Selection = worksheet.Range["B3:F9"];
// Specify the active cell.
worksheet.SelectedCell = worksheet.Cells["E7"];
If a 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.
worksheet.Selection = worksheet.Range["B3:D6"];
worksheet.SelectedCell = worksheet.Range["C5:E7"];
If a 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.
worksheet.Selection = worksheet.Range["B3:D6"];
worksheet.SelectedCell = worksheet.Range["A2:C2"];
Note
Another way to select the cell range in the worksheet is to use the range’s RangeExtensions.Select extension method, defined by the RangeExtensions class. This extension method is accessible as a method of the CellRange object and called by using the instance method syntax.
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 Worksheet.SetSelectedRanges and Worksheet.GetSelectedRanges methods to set or obtain a collection of selected ranges in the worksheet.
Note
The Selection, SelectedCell, ActiveCell, SetSelectedRanges and GetSelectedRanges members of the SpreadsheetControl or SpreadsheetControl object allow you to manage cell selection in the worksheet that is currently active in the control.
Related GitHub Examples
The following code snippets (auto-collected from DevExpress Examples) contain references to the Selection property.
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.