)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: GridControl |
Published Date: April 30, 2011 |
Last Revised Date: December 18, 2019

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

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon