Selection in Windows Forms Spreadsheet

23 Jul 20265 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, the selection behavior is enabled in the Spreadsheet. To disable the selection in the Spreadsheet, set the AllowSelection property to 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 a range,
spreadsheet.ActiveGrid.SelectionController.AddSelection(GridRangeInfo.Cells(4,6,5,8));

//To add the selection for a 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 a 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 the current cell to the specified row and column index,
spreadsheet.ActiveGrid.CurrentCell.MoveCurrentCell(5, 5);

//To move 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 a cell is clicked.

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 that indicates whether to allow the selection in the ActiveGrid.

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 a value indicating whether the grid has a CurrentCell.

PreviousRowColumnIndex

Gets or sets the row and column index of the previous CurrentCell.

Below table lists the methods associated with selection,

Methods Description

AddSelection

Adds or extends the selection to the specified range.

ClearSelection

Clears the Selection.

MoveCurrentCell

Moves the current cell to the specified 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 or last cell of the current cell based on the arrow direction.
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 Extends 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.