Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
SQL
i have 5 columns with data filled , what i need is when i press enter key it should work like tab key ,my last code works but when i edit the cell and then press enter key it moves down to next row and not next cell .
 when i move from one cell to another without entering any data in a cell i should not be able to move to another cell please help me?
For example if i don't dive any value in the cell and if i press enter or tab key i should not be able to move to another cell
Posted

create a custom control inherit from gridview
override below two methods. and use this custom gridview instead of datagridview.
VB
Protected Overrides Function ProcessDataGridViewKey(ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
          Try
              If e.KeyCode = System.Windows.Forms.Keys.Enter Then
                  If MyBase.CurrentCell.ColumnIndex = MyBase.Columns.Count - 1 And MyBase.CurrentCell.RowIndex = MyBase.Rows.Count - 1 Then
                      Dim a As Form
                      a = DirectCast((MyBase.FindForm()), Form)'while last cell of grid it will pass focus to another control on form
                      a.Controls.Find(MyBase.AccessibleDescription, True)(0).Focus()
                  Else
                      MyBase.ProcessTabKey(e.KeyData)
                  End If
                  Return True
              End If
              Return MyBase.ProcessDataGridViewKey(e)
          Catch
          End Try
      End Function


'When edit Particular cell & then press enter
      Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
          If keyData = System.Windows.Forms.Keys.Enter Then
              If MyBase.CurrentCell.ColumnIndex = MyBase.Columns.Count - 1 And MyBase.CurrentCell.RowIndex = MyBase.Rows.Count - 1 Then
                  Dim a As Form
                  a = DirectCast((MyBase.FindForm()), Form)
                  a.Controls.Find(MyBase.AccessibleDescription, True)(0).Focus()
              Else
                  MyBase.ProcessTabKey(keyData)
              End If
              Return True
          End If
          Return MyBase.ProcessDialogKey(keyData)
      End Function

Happy Coding!
:)
 
Share this answer
 
Comments
Sudersan Murthy 4-Aug-13 3:20am    
I am a newbie , can you translate it to c# , thanks so much for your help
Aarti Meswania 4-Aug-13 3:25am    
welcome :)
to convert code in c# just go to below site and copy paste my code, and convert it from vb to c#
http://converter.telerik.com/
Sudersan Murthy 4-Aug-13 3:36am    
Thank You Aarti :)
Aarti Meswania 4-Aug-13 3:40am    
Welcome!
Glad to help you!:)
This is the default behavior of DataGridView already. You don't have to do anything special.

—SA
 
Share this answer
 
Comments
Sudersan Murthy 4-Aug-13 3:54am    
I dont understand
Sergey Alexandrovich Kryukov 4-Aug-13 12:26pm    
I checked on a DataGridView application I have. I press Enter, the selection goes to next cell. This is the default behavior.
—SA
nirav raval 14-Dec-13 8:10am    
This only when you are not Editing any cell.
If you are editing any cell and wanted to move on next object (Cell/Control) you need to use above function.
Member 12365816 17-Jun-16 6:30am    
Please submit this code in C#..............thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900