Cell Navigation in Windows Forms Grid Control

This section explains about the cell navigation in GridControl using Tab, Enter, arrow keys and other combination of keys.

The navigation of cells can be identified through the selection of the CurrentCell or the range of cells. The selection will be changed according to the selection mode set for the GridControl such as range selection and record selection.

The GridControl provides support for all the standard navigation using keyboard. The current cell of the grid can be navigated by using the up, down, left and right arrow keys of the keyboard.

Other navigation keys such as Page Up, Page Down will be used to navigate to the top and bottom row of the view layout of the grid.
The Shift+ Arrow Keys combination is used to include the next cell to the selection at the given direction.
The Ctrl+ Arrow keys combination is used to navigate the current cell to the leftmost, rightmost, topmost and bottommost position.
The Ctrl+Home and Ctrl+End key combinations are used to move the current cell selection to the TopLeft and BottomRight cells of the GridControl.

The GridControl provides the support for navigating the cells using the mouse click. The CurrentCell selection can be navigated to the desired cell by clicking on it. This default option of the grid can be restricted by using the ControllerOptions property.

//Prevent cell navigation using mouse click.
this.gridControl1.ControllerOptions &= ~Syncfusion.Windows.Forms.Grid.GridControllerOptions.SelectCells & ~Syncfusion.Windows.Forms.Grid.GridControllerOptions.ClickCells;
'Prevent cell navigation using mouse click.
Me.gridControl1.ControllerOptions = Me.gridControl1.ControllerOptions And (Not Syncfusion.Windows.Forms.Grid.GridControllerOptions.SelectCells) And Not Syncfusion.Windows.Forms.Grid.GridControllerOptions.ClickCells

Prevent removing of selection using mouse right click

The selection will be cleared when perform the right click on the selected range of cells. It can be restricted from clearing the selection using the SelectCellsMouseButtonsMask property. This will be used to display the context menu for performing the actions on the selected range of cells.

//Restrict the clearing of selection using the mouse right click.
this.gridControl1.SelectCellsMouseButtonsMask = System.Windows.Forms.MouseButtons.Left;
'Restrict the clearing of selection using the mouse right click.
Me.gridControl1.SelectCellsMouseButtonsMask = System.Windows.Forms.MouseButtons.Left

Restrict cell navigation using event

The current cell navigation can be restricted by canceling the CurrentCellActivating event.

this.gridControl1.CurrentCellActivating += new GridCurrentCellActivatingEventHandler(gridControl1_CurrentCellActivating);
void gridControl1_CurrentCellActivating(object sender, Syncfusion.Windows.Forms.Grid.GridCurrentCellActivatingEventArgs e)
{

    //Cancel the activation of current cell.
    e.Cancel = true;
}
Private Me.gridControl1.CurrentCellActivating += New GridCurrentCellActivatingEventHandler(AddressOf gridControl1_CurrentCellActivating)
Private Sub gridControl1_CurrentCellActivating(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridCurrentCellActivatingEventArgs)

	'Cancel the activation of current cell.
	e.Cancel = True
End Sub

Setting TAB key behavior

The cell navigation using TAB key can be enabled in GridControl by setting the WantTabKey property to true.

//Enable Cell Navigation using Tab Key.
this.gridControl.WantTabKey = true;

//Disable Cell Navigation Using Tab Key.
this.gridControl.WantTabKey = false;
'Enable Cell Navigation using Tab Key.
Me.gridControl.WantTabKey = True

'Disable Cell Navigation Using Tab Key.
Me.gridControl.WantTabKey = False

Handling the TAB key to navigate within grid

The WantTabKeyInPreProcessMessage property is used to indicate whether the GridControl should handle TAB keys to move the focus either cells or controls. If it is true then it will make sure that no other control on a form can override the TAB key behavior of this control.

//set true for tab key move between cells right away and restrict to other controls use the tab behavior.
this.gridControl.WantTabKeyInPreProcessMessage = true;
'Set true for tab key move between cells right away and restrict to other controls use the tab behavior.
Me.gridControl.WantTabKeyInPreProcessMessage = True

Setting the TAB index

The TabIndex property is used to set the tab order of the GridControl while using the TAB key navigations.

//The grid will get the focus at the 4th time of pressing the Tab key on the form.
this.gridControl.TabIndex = 4;
'The grid will get the focus at the 4th time of pressing the Tab key on the form.
Me.gridControl.TabIndex = 4

NOTE

The Changing of TabIndex in GridControl can be notified by using the TabIndexChanged event.

Avoid setting focus using TAB key for a particular control

The focus for particular control using TAB key can be restricted by using TabStop property.

//Set true to allow focus using TAB key, Set false to restrict the focus using TAB key.
this.gridControl.TabStop = false;
'Set true to allow focus using TAB key, Set false to restrict the focus using TAB key.
Me.gridControl.TabStop = False

Releasing TAB focus from Grid

When setting the ActiveControl of the form as GridControl, the focus will not move from the grid to other controls in the form using the TAB key. This difficulty can be overcome by setting WantTabKey property to false after setting the ActiveControl. This helps to navigate other controls in the form.

//Setting the ActiveControl to GridControl.
this.ActiveControl = this.gridControl1;
this.gridControl1.WantTabKey = false;
'Setting the ActiveControl to GridControl.
Me.ActiveControl = Me.gridControl1
Me.gridControl1.WantTabKey = False

Disabling Escape key

The grid can reset the cell contents or the cell selection using the Escape key. This can be disabled by setting the WantEscapeKey property to false.

//To avoid the Escape key.
this.gridControl.WantEscapeKey = false;
'To avoid the Escape key.
Me.gridControl.WantEscapeKey = False

Disabling Enter key

The Enter Key can be used to navigate the cells in GridControl when it’s not in editing mode. This can be disabled by setting the WantEnterKey property to false.

//To avoid the Enter key.
this.gridControl.WantEnterKey = false;
'To avoid the Enter key.
Me.gridControl.WantEnterKey = False

Setting Enter key behavior

The navigation of cells using the Enter key can be handled using EnterKeyBehavior property. It provides the following navigation options to move the cell focus when Enter is pressed.

  • Bottom
  • BottomRight
  • Down
  • Left
  • MostLeft
  • MostRight
  • None
  • PageDown
  • PageUp
  • Right
  • Top
  • TopLeft
  • Up
// Cell focus moved to down direction.
this.gridControl.EnterKeyBehavior = GridDirectionType.Down;
'Cell focus moved to down direction.
Me.gridControl.EnterKeyBehavior = GridDirectionType.Down

Setting cell navigation behavior

The EnterKeyBehavior is working based on WrapCellBehavior property. It is used to navigate to first column of the next row or last column of the previous row when at end or beginning of a row.

The GridWrapCellBehavior enumeration has the following list of options,

  • WrapGrid - Move to first row and column, when focus at the last row and last column.
  • WrapRow - Move to first column in next row or last column in previous row.
  • None - Don’t move current cell.
  • NextControlInForm - Activate next sibling control in the dialog when focus at the last row and last column or Activate previous sibling control in the dialog when focus at the first row and column.
//Focus was moved to first row and column, when focus at the last row and last column.
this.gridControl.Model.Options.WrapCellBehavior = GridWrapCellBehavior.WrapGrid;
'Focus was moved to first row and column, when focus at the last row and last column.
Me.gridControl.Model.Options.WrapCellBehavior = GridWrapCellBehavior.WrapGrid

Programmatic cell navigation

The cells navigation through arrow keys can be achieved programmatically by using the MoveUp, MoveDown, MoveLeft and MoveRight methods of the CurrentCell.

//Move the Current Cell to the Up direction.
this.gridControl1.CurrentCell.MoveUp();

//Move the Current Cell to the down direction.
this.gridControl1.CurrentCell.MoveDown();

//Move current cell to the left.
this.gridControl1.CurrentCell.MoveLeft();

//Move Current Cell to the right.
this.gridControl1.CurrentCell.MoveRight();
'Move the Current Cell to the Up direction.
Me.gridControl1.CurrentCell.MoveUp()

'Move the Current Cell to the down direction.
Me.gridControl1.CurrentCell.MoveDown()

'Move current cell to the left.
Me.gridControl1.CurrentCell.MoveLeft()

'Move Current Cell to the right.
Me.gridControl1.CurrentCell.MoveRight()

The current cell can be moved to particular cell by using MoveTo method.

//Move the current cell to specific cell.
this.gridControl1.CurrentCell.MoveTo(GridRangeInfo.Cell(2, 2));
'Move the current cell to specific cell.
Me.gridControl1.CurrentCell.MoveTo(GridRangeInfo.Cell(2, 2))

NOTE

The desired cell can be programmatically get into the view by using the ScrollCellInView method.

Moving current cell without selecting any cells

The current cell can be moved to the specific direction without selecting any cells by using the InternalMove method. It will skip the number of cells given in the argument from the current cell position.

// Used to move the current cell to the specific direction without selection. 
this.gridControl1.CurrentCell.InternalMove(GridDirectionType.Right, 4, GridSetCurrentCellOptions.ScrollInView);
'Used to move the current cell to the specific direction without selection. 
Me.gridControl1.CurrentCell.InternalMove(GridDirectionType.Right, 4, GridSetCurrentCellOptions.ScrollInView)