The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.
.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+
.NET Core 3.0+
Row

Worksheet.Selection Property

Gets or sets a cell range selected in the worksheet.

Namespace: DevExpress.Spreadsheet

Assembly: DevExpress.Spreadsheet.v19.2.Core.dll

Declaration

CellRange Selection { get; set; }
Property Selection As CellRange
CellRange Selection { get; set; }
Property Selection As CellRange
CellRange Selection { get; set; }
Property Selection As CellRange

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.

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.

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.

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 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"];

SpreadsheetControl_Worksheet_Selection_Example1

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"];

SpreadsheetControl_Worksheet_Selection_Example2

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.

Spreadsheet_Worksheet_MultipleSelectedRanges_Example2

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.

See Also