Selection in UWP Spreadsheet (SfSpreadsheet)
23 Jul 20265 minutes to read
This section explains the Selection behavior in SfSpreadsheet.
The SfSpreadsheet control provides support for selection in grid by using mouse, keyboard and touch interactions.
By default, Selection behavior is enabled in SfSpreadsheet, but if you want to disable the selection in SfSpreadsheet, then set the AllowSelection Property to false.
void spreadsheet_WorkbookLoaded(object sender, WorkbookLoadedEventArgs args)
{
spreadsheet.ActiveGrid.AllowSelection = false;
}Accessing the Current cell
SfSpreadsheet allows the user to access the active cell by using the CurrentCell property of the SelectionController Class.
var cell = spreadsheet.ActiveGrid.SelectionController.CurrentCell;Accessing the Selected ranges
SfSpreadsheet allows the user to access the selected ranges from the SpreadsheetGrid using the SelectedRanges property of SpreadsheetGrid.
var rangeList = spreadsheet.ActiveGrid.SelectedRanges;NOTE
To get the active range in the selected ranges list, use
ActiveRangeproperty ofGridRangeInfoListclass.
Adding or Clearing the Selection
SfSpreadsheet allows the user to add and clear the selection in the Active SpreadsheetGrid for the given range.
//To Add the Selection for range,
spreadsheet.ActiveGrid.SelectionController.AddSelection(GridRangeInfo.Cells(4,6,5,8));
//To Add the Selection for particular row,
spreadsheet.ActiveGrid.SelectionController.AddSelection(GridRangeInfo.Row(4));
//To Add the Selection for multiple rows,
spreadsheet.ActiveGrid.SelectionController.AddSelection(GridRangeInfo.Rows(4,9));
//To Add the Selection for particular column,
spreadsheet.ActiveGrid.SelectionController.AddSelection(GridRangeInfo.Col(5));
//To Add the Selection for multiple columns,
spreadsheet.ActiveGrid.SelectionController.AddSelection(GridRangeInfo.Cols(5,10));
//To Clear the Selection,
spreadsheet.ActiveGrid.SelectionController.ClearSelection();Move Current Cell
SfSpreadsheet allows the user to move the current cell to the mentioned cell in SpreadsheetGrid.
//Moves current cell to the mentioned row and column index,
spreadsheet.ActiveGrid.CurrentCell.MoveCurrentCell(5, 5);
//For moving the current cell to a different sheet,
spreadsheet.SetActiveSheet("Sheet2");
spreadsheet.ActiveGrid.CurrentCell.MoveCurrentCell(6, 5);Converting GridRangeInfo into IRange
SfSpreadsheet allows the user to convert the GridRangeInfo into the equivalent IRange by using the ConvertGridRangeToExcelRange method of the GridExcelHelper class.
var excelRange = GridExcelHelper.ConvertGridRangeToExcelRange(GridRangeInfo.Cell(4, 5), spreadsheet.ActiveGrid);TIPS
Users can also convert the
IRangeinto equivalentGridRangeInfoby using theConvertExcelRangeToGridRangemethod of theGridExcelHelperclass.
Properties, Methods and Events
Below table lists the events associated with selection behavior,
| Events | Description |
|---|---|
CellClick
|
Occurs when you click on the cell. |
CurrentCellActivating
|
Occurs when the current cell is going to be activated. This event allows you to cancel the current cell activation. |
CurrentCellActivated
|
Occurs after the current cell is activated. |
SelectionChanging
|
Occurs when the selection is going to be changed. This event allows to cancel the selection change. |
SelectionChanged
|
Occurs after the selection is changed. |
Below table lists the properties associated with selection,
| Properties | Description |
|---|---|
SelectedRanges
|
Gets or sets the collection of selected ranges from grid. |
SelectionBrush
|
Gets or sets the selected area brush. |
SelectionBorderBrush
|
Gets or sets the selection border brush. |
SelectionBorderThickness
|
Gets or sets the thickness of selection border. |
SelectionController
|
Gets the Selection Controller which provides the selection of content when the user drags the pressed mouse to an edge of the control. |
AllowSelection
|
Gets or sets a value indicating whether to allow the selection in the ActiveGrid or not. |
ShowTouchIndicator
|
Determines whether the touch indicator will be shown or not. |
TouchHitTestPrecision
|
Gets or sets the distance of touch precision point from touch indicator. |
Below table lists the properties associated with CurrentCell of SpreadsheetGrid,
| Properties | Description |
|---|---|
CellRowColumnIndex
|
Gets the row and column index of the CurrentCell. |
RowIndex
|
Gets the row index of the CurrentCell. |
ColumnIndex
|
Gets the column index of the CurrentCell. |
Range
|
Gets the range of the CurrentCell. |
HasCurrentCell
|
Gets the value indicating whether the Grid has CurrentCell or not. |
PreviousRowColumnIndex
|
Gets or sets the row and column index of old CurrentCell. |
Below table lists the methods associated with selection,
| Methods | Description |
|---|---|
AddSelection
|
Adds or extends the Selection to the mentioned range. |
ClearSelection
|
Clears the Selection. |
MoveCurrentCell
|
Moves the Current cell to the mentioned row and column index. |
Key Navigation
Below 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 in the worksheet. |
| Down Arrow | Moves one cell down from the current cell in the worksheet. |
| Left Arrow | Moves one cell left from the current cell in the worksheet. |
| Right Arrow | Moves one cell right from the current cell in the worksheet. |
| 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 beginning cell of a worksheet. |
| CTRL+END | Moves to the last cell of a worksheet. |
| ALT+PAGE UP | Moves one screen to the left in a worksheet. |
| ALT+PAGE DOWN | Moves one screen to the right in a worksheet. |
| CTRL+ARROW KEYS | Moves to the first/last cell of the current cell based on Arrow directions . |
| ENTER | Moves the active current cell to one cell down in the selection. |
| SHIFT+ENTER | Moves the active current cell to one cell up in the selection. |
| TAB | Moves the active current cell in one cell to the right of the selection. |
| SHIFT+TAB | Moves the active current cell in one cell to the left of the selection. |
| BACKSPACE | Clears the content of the active cell and begins edit mode when the cell is empty. |
| CTRL+A | Selects the entire worksheet. |
| SHIFT+ARROW KEYS | Extends the selection by one cell in the direction of the arrow key. |
| CTRL+SHIFT+ARROW KEYS | Extend the selection to the last 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. |
| SHIFT+PAGE DOWN | Extends the selection down in a worksheet. |
| SHIFT+PAGE UP | Extends the selection up in a worksheet. |