Skip to main content

SpreadsheetControl.SelectedCell Property

Gets or sets an active cell in the active worksheet.

Namespace: DevExpress.XtraSpreadsheet

Assembly: DevExpress.XtraSpreadsheet.v24.1.dll

NuGet Package: DevExpress.Win.Spreadsheet

Declaration

[Browsable(false)]
public CellRange SelectedCell { get; set; }

Property Value

Type Description
CellRange

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

Remarks

The 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 has focus) that can also be obtained via the SpreadsheetControl.ActiveCell property. The SpreadsheetControl.Selection property specifies the range of cells selected in the active worksheet (B3:F9, in the image below). The Selection can be a single cell or 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 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.

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 SpreadsheetControl.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.

The following code snippets (auto-collected from DevExpress Examples) contain references to the SelectedCell 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.

See Also