Back to Devexpress

SpreadsheetControl.Selection Property

windowsforms-devexpress-dot-xtraspreadsheet-dot-spreadsheetcontrol-2fbff363.md

latest9.4 KB
Original Source

SpreadsheetControl.Selection Property

Gets or sets a cell range selected in the active worksheet.

Namespace : DevExpress.XtraSpreadsheet

Assembly : DevExpress.XtraSpreadsheet.v25.2.dll

NuGet Package : DevExpress.Win.Spreadsheet

Declaration

csharp
[Browsable(false)]
public CellRange Selection { get; set; }
vb
<Browsable(False)>
Public Property Selection As CellRange

Property Value

TypeDescription
CellRange

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

|

Remarks

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

csharp
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"];
vb
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.

csharp
spreadsheetControl1.Selection = activeSheet.Range["B3:D6"];
spreadsheetControl1.SelectedCell = activeSheet.Range["C5:E7"];
vb
spreadsheetControl1.Selection = activeSheet.Range("B3:D6")
spreadsheetControl1.SelectedCell = activeSheet.Range("C5:E7")

If the 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
spreadsheetControl1.Selection = activeSheet.Range["B3:D6"];
spreadsheetControl1.SelectedCell = activeSheet.Range["A2:C2"];
vb
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 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 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.

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

csharp
private void barBtnBindWeatherListToTableUsingTableCollection_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
    BindWeatherReportToFixedTable(MyWeatherReportSource.Data, spreadsheetControl1.Selection);
}

winforms-spreadsheet-bind-to-ms-sql-server-database/CS/SuppliersExample/Form1.cs#L123

csharp
Worksheet sheet = spreadsheetControl1.ActiveWorksheet;
CellRange selectedRange = spreadsheetControl1.Selection;
CellRange boundRange = sheet.DataBindings[0].Range;

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

vb
Private Sub barBtnBindWeatherListToTableUsingTableCollection_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barBtnBindWeatherListToTableUsingTablecCollection.ItemClick
    BindWeatherReportToFixedTable(MyWeatherReportSource.Data, spreadsheetControl1.Selection)
End Sub

winforms-spreadsheet-bind-to-ms-sql-server-database/VB/SuppliersExample/Form1.vb#L117

vb
Dim sheet As Worksheet = spreadsheetControl1.ActiveWorksheet
Dim selectedRange As CellRange = spreadsheetControl1.Selection
Dim boundRange As CellRange = sheet.DataBindings(0).Range

See Also

SelectedCell

ActiveCell

SetSelectedRanges

GetSelectedRanges()

SpreadsheetControl Class

SpreadsheetControl Members

DevExpress.XtraSpreadsheet Namespace