Grid / Grid Features / Selection
Selection
 About Selection

The selection of the grid is exposed by a NGridSelection attribute, that is accessible from the Selection property of NGrid. The grid selection is organized to work around grid rows and to maintain the notion of a Current Row and Current Cell.

The grid selection is also responsible for performing a variety of navigation commands that can alter the current cell, select/deselect rows and also change the current cell. The grid selection also exposes properties that control the events that trigger the current cell editing.

The selected rows and current cell are visually demonstrated on the following image:

The rest of the topic discusses all these aspects of the grid selection in details. All code examples work with an NGrid instance. To get the grid shown in a grid view, use its Grid property:

Getting the grid of a grid view
Copy Code
NGrid grid = gridView.Grid;
 Current Cell and Current Row

The current cell is exposed by the CurrentCell property. The following code example changes the current cell to be the first cell of a specific row:

Current Cell
Copy Code
// Get the data row for the data source record number 10
NDataRow row = grid.GetDataRow(10);

// Get the first cell of the row
row.EnsureContentCreated();
NCell cell = row.GetCellAt(0);

// Change the current cell
grid.Selection.CurrentCell = cell;

Changing the current cell also changes the current row of the selection. The current row is exposed by the CurrentRow property. Setting the current row property automatically resets the CurrentCell property to null. The following code example changes the current row and resets the current cell:

Current Row
Copy Code
// Get the data row for the data source record number 10
NDataRow row = grid.GetDataRow(10);

// Change the current row
grid.Selection.CurrentRow = row;

The grid can be configured to operate in row selection mode only - that is does not allow the user to navigate around the individual row cells. This is achieved by setting the AllowCurrentCell property to false (by default set to true).

 Selection Events

To get notified when the current row or the current cell changes subscribe to the following events of the grid's Selection object:

  • CurrentRowChanged - raised when the current row has changed.
  • CurrentCellChanged - raised when the current cell has changed.

The following piece of code demonstrates how to subscribe and handle the CurrentRowChanged event:

Handling the CurrentRowChanged event
Copy Code
grid.Selection.CurrentRowChanged += OnGridSelectionCurrentRowChanged;

private void OnGridSelectionCurrentRowChanged(NCurrentRowChangedEventArgs arg)
{
    NRow currentRow = arg.NewCurrentRow;
}
 Row Selection Operations

The grid selection can be configured to operate in two modes via its Mode property, which are:

  • Single - only a single row can be selected
  • Multiple - multiple rows can be selected

The selection object provides a powerful set of methods, which can help you modify the selection. The most important ones are demonstrated in the following example:

Row Selection Operations
Copy Code
// get the grid selection
NGridSelection selection = grid.Selection;
// make row1 the one and only selected node
selection.SingleSelect(row1);
// append row2 to the current selection
selection.MultiSelect(row2);
// toggle the selection state of the row3 (in this case this will select row3)
selection.ToggleSelect(row3);
// toggle the selection state of the row3 (in this case this will deselect row3)
selection.ToggleSelect(row3);
// deselect row1
selection.Deselect(row1);
// deselect all rows (in this case only row2)
selection.DeselectAll();
// get the selected rows
NList<NRow> selectedRows = selection.SelectedItems;
 Navigation and Editing Commands

The selection object provides a complete set of methods that help you perform different navigation and editing commands. These commands are usually performed in response to different user actions via the NSelectionNavigationTool. The following table outlines the methods and the keyboard commands in response to which they are usually performed:

Method Description Keyboard Shortcut

Arrow Navigation

MoveUp() Changes the current row to previous navigable row and tries to preserve the current cell column. Up Arrow
MoveDown() Changes the current row to next navigable row and tries to preserve the current cell column. Down Arrow
MoveLeft() Changes the current cell to the cell that is on the left of the current one. If there is no current cell tries to collapse the current row. Left Arrow
MoveRight() Changes the current cell to the cell that is on the right of the current one. If there is no current cell tries to expand the current row. Right Arrow
MoveUpFirst() Moves the current cell up to the first navigable cell in the current column CTRL + Up Arrow
MoveDownLast() Moves the current cell down to the last navigable cell in the current column CTRL + Down Arrow
MoveRowStart() Moves the current cell to the first cell in the row. CTRL + Left Arrow
MoveRowEnd() Moves the current cell to the last cell in the row. CTRL + Right Arrow

Extended Navigation

MoveRowStart() Moves the current cell to the first cell in the row. Home
MoveRowEnd() Moves the current cell to the last cell in the row. End
MovePageDown() Moves the current/selected row to a row that is one page down. Page Down
MovePageUp() Moves the current/selected row to a row that is one page up. Page Up
MoveFirstCell() Moves the current cell to the first navigable cell in the grid. CTRL + Home
MoveLastCell() Moves the current cell to the last navigable cell in the grid. CTRL + End

Editing Commands

TryCurrentCellBeginEdit() Tries to begin the editing of the current cell. F2
TryCurrentCellCancelEdit() Tries to cancel the editing of the current cell. ESC
TryCurrentCellCommitEdit() Tries to commit the editing of the current cell. CTRL + Enter
MoveRightEdit() Same as the move right command, except that it also begins editing of the new current cell, if the previous one was in edit mode. Tab
MoveLeftEdit() Same as the move left command, except that it also begins editing of the new current cell, if the previous one was in edit mode. SHIFT + Tab
MoveDownEdit() Moves the current cell to the navigable cell below it in the grid. If the current cell was edited, starts editing of the new current cell. Enter

 

The selection object also exposes the following properties that affect the editing behavior of the grid:

Property Description
BeginEditCellOnClick Gets or sets whether cell editing should begin when the cell is clicked.
BeginEditCellOnDoubleClick Gets or sets whether cell editing should begin when the cell is double clicked.
BeginEditCellOnBecomeCurrent Gets or sets whether cell editing should begin when the cell becomes the current cell.