windowsforms-devexpress-dot-xtraspreadsheet-dot-spreadsheetcontrol-ee0e900a.md
Gets or sets an active cell in the active worksheet.
Namespace : DevExpress.XtraSpreadsheet
Assembly : DevExpress.XtraSpreadsheet.v25.2.dll
NuGet Package : DevExpress.Win.Spreadsheet
[Browsable(false)]
public CellRange SelectedCell { get; set; }
<Browsable(False)>
Public Property SelectedCell As CellRange
| 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.
|
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.
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"];
Imports DevExpress.Spreadsheet
' ...
Dim workbook As IWorkbook = spreadsheetControl1.Document
Dim activeSheet As Worksheet = 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"];
spreadsheetControl1.Selection = activeSheet.Range("B3:D6")
spreadsheetControl1.SelectedCell = activeSheet.Range("C5:E7")
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"];
spreadsheetControl1.Selection = activeSheet.Range("B3:D6")
spreadsheetControl1.SelectedCell = activeSheet.Range("A2:C2")
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.
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.
bool shouldCopyFormat = biFormatPainter.Checked;
sourceCell = shouldCopyFormat ? spreadsheetControl.SelectedCell : null;
winforms-spreadsheet-bind-to-ms-sql-server-database/CS/SuppliersExample/Form1.cs#L117
sheet.Rows.Unhide(2, 9);
spreadsheetControl1.SelectedCell = sheet["C4"];
}
winforms-spreadsheet-use-custom-cell-editors/CS/DevAVInvoicing/Form1.cs#L190
InitializeRecord(itemRange);
spreadsheetControl1.SelectedCell = itemRange[1];
}
winforms-spreadsheet-spell-check-cell-text/CS/SpreadsheetSpellchecking/Form1.cs#L75
activeSheet.Columns[1].WidthInPixels = 150;
spreadsheetControl.SelectedCell = activeSheet["B2"];
spreadsheetControl.SelectedCell.Value = "Missspelled wods";
Dim shouldCopyFormat As Boolean = biFormatPainter.Checked
sourceCell = If(shouldCopyFormat, spreadsheetControl.SelectedCell, Nothing)
formatPainterMode = If(shouldCopyFormat, FormatPainterModeType.SingleAction, FormatPainterModeType.None)
winforms-spreadsheet-bind-to-ms-sql-server-database/VB/SuppliersExample/Form1.vb#L111
If Not sheet.Rows(4).Visible Then sheet.Rows.Unhide(2, 9)
spreadsheetControl1.SelectedCell = sheet("C4")
End Sub
winforms-spreadsheet-use-custom-cell-editors/VB/DevAVInvoicing/Form1.vb#L177
InitializeRecord(itemRange)
spreadsheetControl1.SelectedCell = itemRange(1)
Finally
winforms-spreadsheet-spell-check-cell-text/VB/SpreadsheetSpellchecking/Form1.vb#L82
activeSheet.Columns(1).WidthInPixels = 150
spreadsheetControl.SelectedCell = activeSheet("B2")
spreadsheetControl.SelectedCell.Value = "Missspelled wods"
See Also