Articles in this section
Category / Section

How to make the 'TAB' key move only within a selected range of cells in WinForms GridControl?

2 mins read

Navigate the cells using Tab key within selected range

You can achieve this by handling the Grid control's keydown and when the keydata is a 'TAB' key, move the current cell within the selected cells.

C#

void gridControl1_KeyDown(object sender, KeyEventArgs e)
{
   // Check for TAB key
   if (e.KeyData == Keys.Tab)
   {
      GridCurrentCell cc = this.gridControl1.CurrentCell;
      GridRangeInfoList list;
      if (this.gridControl1.Selections.GetSelectedRanges(out list, true))
      {
         // Don't consider the currentcell as selection
         GridRangeInfo range2 = new GridRangeInfo();
         range2 = GridRangeInfo.Cell(cc.RowIndex, cc.ColIndex);
         if (!this.gridControl1.Selections.Ranges.ActiveRange.Equals(range2))
           {
              foreach (GridRangeInfo range in list)
              {
                 GridRangeInfo range1 = range.ExpandRange(range.Top, range.Left, range.Bottom, range.Right);
                 if (this.gridControl1.CurrentCell.ColIndex < range1.Right)
                 {
                    this.gridControl1.CurrentCell.MoveRight();
                    e.Handled = true;
                 }
                 else if (this.gridControl1.CurrentCell.ColIndex == range1.Right)
                 {
                    if (this.gridControl1.CurrentCell.RowIndex < range1.Bottom)
                    {
                      this.gridControl1.CurrentCell.MoveTo(this.gridControl1.CurrentCell.RowIndex + 1, range.Left);
                      e.Handled = true;
                    }
                    else if (this.gridControl1.CurrentCell.RowIndex == range1.Bottom)
                    {
                       this.gridControl1.CurrentCell.MoveTo(range1.Top, range1.Left);
                       e.Handled = true;
                    }
                 }
              }
          }
      }
   }
}

VB

Private Sub gridControl1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
   ' Check for TAB key
   If e.KeyData = Keys.Tab Then
     Dim cc As GridCurrentCell = Me.gridControl1.CurrentCell
     Dim list As GridRangeInfoList
     If Me.gridControl1.Selections.GetSelectedRanges(list, True) Then
       ' Don't consider the currentcell as selection
       Dim range2 As New GridRangeInfo()
       range2 = GridRangeInfo.Cell(cc.RowIndex, cc.ColIndex)
       If Not Me.gridControl1.Selections.Ranges.ActiveRange.Equals(range2) Then
         For Each range As GridRangeInfo In list
             Dim range1 As GridRangeInfo = range.ExpandRange(range.Top, range.Left, range.Bottom, range.Right)
             If Me.gridControl1.CurrentCell.ColIndex < range1.Right Then
               Me.gridControl1.CurrentCell.MoveRight()
               e.Handled = True
             ElseIf Me.gridControl1.CurrentCell.ColIndex = range1.Right Then
                If Me.gridControl1.CurrentCell.RowIndex < range1.Bottom Then
                   Me.gridControl1.CurrentCell.MoveTo(Me.gridControl1.CurrentCell.RowIndex + 1, range.Left)
                   e.Handled = True
                ElseIf Me.gridControl1.CurrentCell.RowIndex = range1.Bottom Then
                   Me.gridControl1.CurrentCell.MoveTo(range1.Top, range1.Left)
                   e.Handled = True
                End If
            End If
         Next range
      End If
   End If
  End If
End Sub

 

Samples:

C#: TAB key Navigation

VB: TAB key Navigation

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied