Selection in Windows Forms Spreadsheet

9 Oct 20235 minutes to read

This section explains about the Selection behavior in Spreadsheet.

The Spreadsheet control provides support for selection in grid by using mouse, keyboard and touch interactions.

By default, Selection behavior will be enabled in Spreadsheet,but if you want to disable the selection in Spreadsheet, then set the AllowSelection Property to be false.

void spreadsheet_WorkbookLoaded(object sender, WorkbookLoadedEventArgs args)
{
    spreadsheet.ActiveGrid.AllowSelection = false;
}

Accessing the current cell

Spreadsheet allows the user to access the active cell by using the CurrentCell property of SelectionController Class.

var cell= spreadsheet.ActiveGrid.SelectionController.CurrentCell;

Accessing the selected ranges

Spreadsheet allows the user to access the selected ranges of the SpreadsheetGrid using SelectedRanges property of SpreadsheetGrid.

var rangeList = spreadsheet.ActiveGrid.SelectedRanges;

NOTE

To get the active range in the selected ranges list, use ActiveRange property of GridRangeInfoList class.

Adding or clearing the selection

Spreadsheet 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();

Moving current cell

Spreadsheet allows the user to move the current cell to the mentioned cell in SpreadsheetGrid.

//Moves current cell to the mentioned row and column index of cell,
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

Spreadsheet allows the user to convert the GridRangeInfo into the equivalent IRange by using ConvertGridRangeToExcelRange method of GridExcelHelper class.

var excelRange = GridExcelHelper.ConvertGridRangeToExcelRange(GridRangeInfo.Cell(4, 5), spreadsheet.ActiveGrid);

TIPS

Users can also convert the IRange into equivalent GridRangeInfo by using ConvertExcelRangeToGridRange method of GridExcelHelper class.

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 allow 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 the value 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,

</tr </table> Below table lists the methods associated with selection,
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.
Methods Description

AddSelection

Adds/Extends the Selection to the mentioned range .

ClearSelection

Clears the Selection.

MoveCurrentCell

Move the Current cell to 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 .
UPARROW Moves to one cell up of the current cell in the worksheet.
DOWNARROW Moves to one cell down of the current cell in the worksheet.
LEFTARROW Moves to one cell left of the current cell in the worksheet.
RIGHTARROW Moves to one cell right of the current cell in the worksheet.
PAGEUP Moves to the first visible cell of the current column.
PAGEDOWN 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 Begins the edit operation for the active cell in the selection.
CTRL+A Selects the entire worksheet.
SHIFT+ARROW KEYS Extends the selection by one cell based on the arrow direction.
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.
SHIFT+PAGE DOWN Extends the selection down in a worksheet.
SHIFT+PAGE UP Extends the selection up in a worksheet.