Back to Devexpress

Worksheet.Selection Property

officefileapi-devexpress-dot-spreadsheet-dot-worksheet-16701e09.md

latest11.2 KB
Original Source

Worksheet.Selection Property

Gets or sets a cell range selected in the worksheet.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

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

Property Value

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

csharp
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"];
vb
Imports DevExpress.Spreadsheet
' ...

Dim workbook As IWorkbook = spreadsheetControl1.Document
Dim worksheet As 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.

csharp
worksheet.Selection = worksheet.Range["B3:D6"];
worksheet.SelectedCell = worksheet.Range["C5:E7"];
vb
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.

csharp
worksheet.Selection = worksheet.Range["B3:D6"];
worksheet.SelectedCell = worksheet.Range["A2:C2"];
vb
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.

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.

winforms-spreadsheet-control-create-a-data-entry-form/CS/DataEntryFormSample/MainForm.cs#L49

csharp
if (sheet != null) {
    var editors = sheet.CustomCellInplaceEditors.GetCustomCellInplaceEditors(sheet.Selection);
    if (editors.Count == 1)

how-to-export-cell-range-to-a-datatable/CS/ExportToDataTableExample/Form1.cs#L29

csharp
Worksheet worksheet = spreadsheetControl1.Document.Worksheets.ActiveWorksheet;
CellRange range = worksheet.Selection;
bool rangeHasHeaders = this.barCheckItemHasHeaders1.Checked;

wpf-spreadsheet-control-create-a-data-entry-form/CS/WpfDataEntryFormSample/MainWindow.xaml.cs#L83

csharp
{
    var editors = sheet.CustomCellInplaceEditors.GetCustomCellInplaceEditors(sheet.Selection);
    if (editors.Count == 1)

winforms-spreadsheet-use-cell-range-as-data-source/CS/RangeDataSource/Form1.cs#L30

csharp
Worksheet sheet = spreadsheetControl1.ActiveWorksheet;
CellRange dataRange = sheet.Selection;
if (dataRange.Equals(sheet.Tables[0].Range))

how-to-bind-a-worksheet-to-a-generic-list-or-a-bindinglist-data-source/CS/DataBindingToListExample/Form1.cs#L130

csharp
Worksheet sheet = spreadsheetControl1.Document.Worksheets.ActiveWorksheet;
var bindings = sheet.DataBindings.GetDataBindings(sheet.Selection);
string message = "No data bindings found";

winforms-spreadsheet-control-create-a-data-entry-form/VB/DataEntryFormSample/MainForm.vb#L75

vb
If sheet IsNot Nothing Then
    Dim editors = sheet.CustomCellInplaceEditors.GetCustomCellInplaceEditors(sheet.Selection)
    If editors.Count = 1 Then

how-to-export-cell-range-to-a-datatable/VB/ExportToDataTableExample/Form1.vb#L30

vb
Dim worksheet As Worksheet = spreadsheetControl1.Document.Worksheets.ActiveWorksheet
Dim range As CellRange = worksheet.Selection
Dim rangeHasHeaders As Boolean = barCheckItemHasHeaders1.Checked

wpf-spreadsheet-control-create-a-data-entry-form/VB/WpfDataEntryFormSample/MainWindow.xaml.vb#L82

vb
If sheet IsNot Nothing Then
    Dim editors = sheet.CustomCellInplaceEditors.GetCustomCellInplaceEditors(sheet.Selection)
    If editors.Count = 1 Then

winforms-spreadsheet-use-cell-range-as-data-source/VB/RangeDataSource/Form1.vb#L34

vb
Dim sheet As Worksheet = spreadsheetControl1.ActiveWorksheet
Dim dataRange As CellRange = sheet.Selection
If dataRange.Equals(sheet.Tables(0).Range) Then

how-to-bind-a-worksheet-to-a-generic-list-or-a-bindinglist-data-source/VB/DataBindingToListExample/Form1.vb#L127

vb
Dim sheet As Worksheet = spreadsheetControl1.Document.Worksheets.ActiveWorksheet
Dim bindings = sheet.DataBindings.GetDataBindings(sheet.Selection)
Dim message As String = "No data bindings found"

See Also

SelectedCell

SetSelectedRanges

GetSelectedRanges()

Worksheet Interface

Worksheet Members

DevExpress.Spreadsheet Namespace