Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Moving the focus to the next cell in a datagridview in visual basic 2008 when pressing enter key
 

Moving the focus to the next cell in a datagridview in visual basic 2008 when pressing enter key

Hello,

I've 2 questions:
1-How to make the first column in a datagridview visible but has no focus when moving to a new row in a datagrid? Therefore,I want to give always the focus to the second column when moving to a new row.

2-I am using visual studio 2008 and I wantthe enter key move to the next cell in a datagridview instead of the tab key.I searched over and over and I've found a solution that overrides the 2 functionsProcessDialogKey and OnKeyDown.Is there any other recent solution in visual basic 2008?


The code that I've found is:

ClassInherits DataGridView

Protected Overloads Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean

If keyData = Keys.Enter Then

Dim col As Integer = Me.CurrentCell.ColumnIndex

Dim row As Integer = Me.CurrentCell.RowIndex

If Not (row = Me.NewRowIndex) Then

If col = (Me.Columns.Count - 1) Then

End If

Me.CurrentCell = Me(col + 1, row)

End If

Return True

End If

Return MyBase.ProcessDialogKey(keyData)

End Function

Protected Overloads Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)

If e.KeyData = Keys.Enter Then

Dim col As Integer = Me.CurrentCell.ColumnIndex

Dim row As Integer = Me.CurrentCell.RowIndex

If Not (row = Me.NewRowIndex) Then

If col = (Me.Columns.Count - 1) Then

End If

Me.CurrentCell = Me(col + 1, row)

End If

True

End If

MyBase.OnKeyDown(e)

End Sub

Class


Sam85
sam.m  Wednesday, January 14, 2009 12:39 PM

Hi sam.m,

I am not sure I have fully understood your meaning.
The sample is great, you can change it only a little to achieve your first requirement.

1 ClassmyDataGridView
2 InheritsDataGridView
3
4 ProtectedOverloadsOverridesFunctionProcessDialogKey(ByValkeyDataAsKeys)AsBoolean
5 IfkeyData=Keys.EnterThen
6 DimcolAsInteger=Me.CurrentCell.ColumnIndex
7 DimrowAsInteger=Me.CurrentCell.RowIndex
8 IfNot(row=Me.NewRowIndex)Then
9 Ifcol=(Me.Columns.Count-1)Then
10 col=0
11 row+=1
12 EndIf
13 Me.CurrentCell=Me(col+1,row)
14 EndIf
15 ReturnTrue
16 EndIf
17 ReturnMyBase.ProcessDialogKey(keyData)
18 EndFunction
19
20 ProtectedOverloadsOverridesSubOnKeyDown(ByValeAsKeyEventArgs)
21 Ife.KeyData=Keys.EnterThen
22 DimcolAsInteger=Me.CurrentCell.ColumnIndex
23 DimrowAsInteger=Me.CurrentCell.RowIndex
24 IfNot(row=Me.NewRowIndex)Then
25 Ifcol=(Me.Columns.Count-1)Then
26 col=0
27 row+=1
28 EndIf
29 Me.CurrentCell=Me(col+1,row)
30 EndIf
31 e.Handled=True
32 EndIf
33 MyBase.OnKeyDown(e)
34 EndSub
35 EndClass


After you change "col = -1" to "col = 0", the first column won't be focus when you press enter key to walkthrough all cell. Does this what your purpose? If it doesn't, please feel free to tell me.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Thursday, January 15, 2009 9:53 AM

Hi sam.m,

I am not sure I have fully understood your meaning.
The sample is great, you can change it only a little to achieve your first requirement.

1 ClassmyDataGridView
2 InheritsDataGridView
3
4 ProtectedOverloadsOverridesFunctionProcessDialogKey(ByValkeyDataAsKeys)AsBoolean
5 IfkeyData=Keys.EnterThen
6 DimcolAsInteger=Me.CurrentCell.ColumnIndex
7 DimrowAsInteger=Me.CurrentCell.RowIndex
8 IfNot(row=Me.NewRowIndex)Then
9 Ifcol=(Me.Columns.Count-1)Then
10 col=0
11 row+=1
12 EndIf
13 Me.CurrentCell=Me(col+1,row)
14 EndIf
15 ReturnTrue
16 EndIf
17 ReturnMyBase.ProcessDialogKey(keyData)
18 EndFunction
19
20 ProtectedOverloadsOverridesSubOnKeyDown(ByValeAsKeyEventArgs)
21 Ife.KeyData=Keys.EnterThen
22 DimcolAsInteger=Me.CurrentCell.ColumnIndex
23 DimrowAsInteger=Me.CurrentCell.RowIndex
24 IfNot(row=Me.NewRowIndex)Then
25 Ifcol=(Me.Columns.Count-1)Then
26 col=0
27 row+=1
28 EndIf
29 Me.CurrentCell=Me(col+1,row)
30 EndIf
31 e.Handled=True
32 EndIf
33 MyBase.OnKeyDown(e)
34 EndSub
35 EndClass


After you change "col = -1" to "col = 0", the first column won't be focus when you press enter key to walkthrough all cell. Does this what your purpose? If it doesn't, please feel free to tell me.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Thursday, January 15, 2009 9:53 AM

You can use google to search for other answers

Custom Search

More Threads

• Update through SQLDataAdapter
• DataGridView - BeginUpdate
• Help removing indicator/pointer in row header of datagridview
• Strategies for Showing / Notifying Users of New Data?
• Column Order in Databind
• adding Rows Error
• Binding a DataGridList to existing custom data via the "custom business object" approach?
• How to change from add to update when adding a duplicate row to a data grid using bound source
• Hide item in ComboBox
• Regarding ComboBox column in DataGridView and Tool Tip