Selection in WPF Spreadsheet (SfSpreadsheet)
23 Jul 20265 minutes to read
This section explains the Selection behavior in the WPF Spreadsheet Editor.
The SfSpreadsheet control provides selection support in the grid by using mouse, keyboard, and touch interactions.
By default, selection is enabled in SfSpreadsheet. To disable selection, set the AllowSelection property to false.
void spreadsheet_WorkbookLoaded(object sender, WorkbookLoadedEventArgs args)
{
spreadsheet.ActiveGrid.AllowSelection = false;
}Accessing the Current Cell
The current cell is exposed through the CurrentCell property of the SelectionController class.
var cell = spreadsheet.ActiveGrid.SelectionController.CurrentCell;Accessing the Selected Ranges
The selected ranges of the SpreadsheetGrid are exposed through the SelectedRanges property.
var rangeList = spreadsheet.ActiveGrid.SelectedRanges;NOTE
To get the active range within the selected ranges list, use the ActiveRange property of the GridRangeInfoList class.
Adding or Clearing the Selection
The active SpreadsheetGrid allows you to add new selection ranges and clear the existing selection programmatically.
// Add a selection for the cell range.
spreadsheet.ActiveGrid.SelectionController.AddSelection(GridRangeInfo.Cells(4, 6, 5, 8));
// Add a selection for a single row.
spreadsheet.ActiveGrid.SelectionController.AddSelection(GridRangeInfo.Row(4));
// Add a selection for multiple rows.
spreadsheet.ActiveGrid.SelectionController.AddSelection(GridRangeInfo.Rows(4, 9));
// Add a selection for a single column.
spreadsheet.ActiveGrid.SelectionController.AddSelection(GridRangeInfo.Col(5));
// Add a selection for multiple columns.
spreadsheet.ActiveGrid.SelectionController.AddSelection(GridRangeInfo.Cols(5, 10));
// Clear the current selection.
spreadsheet.ActiveGrid.SelectionController.ClearSelection();Move Current Cell
Move the current cell to a specified cell in the SpreadsheetGrid using the MoveCurrentCell method.
// Moves current cell to the mentioned row and column index of the cell.
spreadsheet.ActiveGrid.CurrentCell.MoveCurrentCell(5, 5);
// Move the current cell to a different sheet.
spreadsheet.SetActiveSheet("Sheet2");
spreadsheet.ActiveGrid.CurrentCell.MoveCurrentCell(6, 5);Converting GridRangeInfo into IRange
Convert a GridRangeInfo value to its equivalent XlsIO IRangeusing the ConvertGridRangeToExcelRange method of the GridExcelHelper class.
var excelRange = GridExcelHelper.ConvertGridRangeToExcelRange(GridRangeInfo.Cell(4, 5), spreadsheet.ActiveGrid);TIPS
You can also convert an
IRangeinto its equivalentGridRangeInfoby using the ConvertExcelRangeToGridRange method of GridExcelHelper.
Properties, Methods, and Events
The tables below list the events, properties, and methods associated with selection behavior.
Events
| Events | Description |
|---|---|
| Occurs when you click a cell. | |
| Occurs when the current cell is about to be activated. This event allows you to cancel the current cell activation. | |
| Occurs after the current cell is activated. | |
| Occurs when the selection is about to change. This event allows you to cancel the selection change. | |
| Occurs after the selection changes. |
Selection properties
| Properties | Description |
|---|---|
| Gets or sets the collection of selected ranges from the grid. | |
| Gets or sets the brush used to fill the selected area. | |
| Gets or sets the selection border brush. | |
| Gets or sets the thickness of the selection border. | |
| Gets the selection controller, which provides the selection of content when the user drags the pressed mouse to an edge of the control. | |
| Gets or sets a value indicating whether selection is allowed in the ActiveGrid. | |
| Gets or sets a value indicating whether the touch indicator is shown. | |
| Gets or sets the distance, of the touch precision point from the touch indicator. |
CurrentCell properties
| Properties | Description |
|---|---|
| Gets the row and column index of the CurrentCell. | |
| Gets the row index of the CurrentCell. | |
| Gets the column index of the CurrentCell. | |
| Gets the range of the CurrentCell. | |
| Gets a value indicating whether the grid has a CurrentCell. | |
| Gets the row and column index of the previous CurrentCell. |
Methods
| Methods | Description |
|---|---|
| Adds or extends the selection to the specified range. | |
| Clears the selection. | |
| Moves the current cell to the specified row and column index. |
Key Navigation
The following table lists the key combinations associated with selection.
| Key Combination | Description |
|---|---|
| Home | Moves to the first cell of the current row. |
| End | Moves to the last cell of the current row. |
| Up Arrow | Moves one cell up from the current cell. |
| Down Arrow | Moves one cell down from the current cell. |
| Left Arrow | Moves one cell left from the current cell. |
| Right Arrow | Moves one cell right from the current cell. |
| Page Up | Moves to the first visible cell of the current column. |
| Page Down | Moves to the last visible cell of the current column. |
| Ctrl+Home | Moves to the first cell of the worksheet (A1). |
| Ctrl+End | Moves to the last used cell of the worksheet. |
| Alt+Page Up | Moves one screen to the left in the worksheet. |
| Alt+Page Down | Moves one screen to the right in the worksheet. |
| Ctrl+Arrow Keys | Moves to the first or last contiguous data cell in the arrow direction. |
| Enter | Moves the active current cell one cell down within the selection. |
| Shift+Enter | Moves the active current cell one cell up within the selection. |
| Tab | Moves the active current cell one cell to the right within the selection. |
| Shift+Tab | Moves the active current cell one cell to the left within the selection. |
| Backspace | Begins the edit operation for the active cell. |
| Ctrl+A | Selects the entire worksheet. |
| Shift+Arrow Keys | Extends the selection by one cell in the arrow direction. |
| Ctrl+Shift+Arrow Keys | Extends the selection to the last contiguous data cell in a row or column. |
| Shift+Home | Extends the selection to the first column from the active cell. |
| Ctrl+Shift+Home | Extends the selection from the active cell to the first cell of the worksheet (A1). |
| Shift+Page Down | Extends the selection down one screen in the worksheet. |
| Shift+Page Up | Extends the selection up one screen in the worksheet. |
NOTE
You can refer to our WPF Spreadsheet Editor feature tour page for its groundbreaking feature representations. You can also explore our WPF Spreadsheet example to know how to render and configure the spreadsheet.