Back to Devexpress

Worksheet.SetSelectedRanges(IList<CellRange>, Boolean) Method

officefileapi-devexpress-dot-spreadsheet-dot-worksheet-dot-setselectedranges-x28-system-dot-collections-dot-generic-dot-ilist-devexpress-dot-spreadsheet-dot-cellrange-system-dot-boolean-x29.md

latest4.1 KB
Original Source

Worksheet.SetSelectedRanges(IList<CellRange>, Boolean) Method

Selects cell ranges in the worksheet and specifies whether the selection expands to fit merged cells.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
bool SetSelectedRanges(
    IList<CellRange> ranges,
    bool expandToMergedCellsSize
)
vb
Function SetSelectedRanges(
    ranges As IList(Of CellRange),
    expandToMergedCellsSize As Boolean
) As Boolean

Parameters

NameTypeDescription
rangesIList<CellRange>

A list of the CellRange objects.

| | expandToMergedCellsSize | Boolean |

true , to expand the selection to include merged cells; otherwise, false.

|

Returns

TypeDescription
Boolean

true , if cell ranges are selected successfully; otherwise false.

|

Remarks

To select multiple non-adjacent cells or cell ranges in the worksheet simultaneously, use the SetSelectedRanges method. The expandToMergedCellsSize parameter enables you to determine whether to create a selection including merged cells, as illustrated below.

csharp
Worksheet sheet = spreadsheetControl1.ActiveWorksheet;
sheet.MergeCells(sheet.Range["E5:F9"]);
List<CellRange> mylist = new List<CellRange>();
mylist.Add(sheet.Range["A2:C4"]);
mylist.Add(sheet.Range["C6:C9"]);
mylist.Add(sheet.Range["E3:E7"]);
sheet.SetSelectedRanges(mylist, true);
vb
Dim sheet As Worksheet = spreadsheetControl1.ActiveWorksheet
sheet.MergeCells(sheet.Range("E5:F9"))
Dim mylist As New List(Of CellRange)()
mylist.Add(sheet.Range("A2:C4"))
mylist.Add(sheet.Range("C6:C9"))
mylist.Add(sheet.Range("E3:E7"))
sheet.SetSelectedRanges(mylist, True)

The code results in a selection that includes merged cells, as shown in the following picture.

If the expandToMergedCellsSize parameter in the SetSelectedRanges method is set to false , the selection does not expand, as shown below:

When you call the SetSelectedRanges method, an active cell is automatically set to the top left cell of the first range in the passed list.

The Worksheet.Selection property specifies the range of cells selected in the worksheet. The Worksheet.SelectedCell property specifies a single cell where data is inserted when an end-user types (in other words, this is the active cell). 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 the cell you assign to SelectedCell is outside the currently selected range, the selection will be changed - it will coincide with the specified active cell.

See Also

Worksheet Interface

Worksheet Members

DevExpress.Spreadsheet Namespace