Microsoft KB Archive/291191

= How To Use the ENTER Key to Navigate in the Visual Basic DataGrid Control =

Article ID: 291191

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q291191



SUMMARY
Many applications that display data in a grid interface allow the user to use the ENTER (RETURN) key to navigate from cell to cell. The Visual Basic DataGrid does not offer a property setting to activate such a feature. This article demonstrates how you can work around this limitation to achieve ENTER key navigation by using simple code.



MORE INFORMATION
The Visual Basic DataGrid normally expects the user to use the ARROW keys or the TAB key to navigate from cell to cell and from row to row. (The exact behavior of the TAB key depends on your choices for the TabAction and WrapCellPointer properties of the DataGrid.) In order to allow the use of the ENTER key for this purpose, the developer must trap the ENTER key press in the DataGrid's KeyDown or KeyPress event and &quot;convert&quot; the pressed key value to a TAB key:  On the DataGrid's Property pages, on the Keyboard tab, select the WrapCellPointer checkbox and select dbgGridNavigation from the TabAction dropdown list. Alternatively, you can set these two properties in your code.  If you do not have a command button on your form with the Default property set to True (which would cause the ENTER key to fire the command button's click procedure), you can trap the ENTER key in the DataGrid's KeyDown event as follows: Private Sub DataGrid1_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyReturn Then KeyCode = vbKeyTab End If End Sub Otherwise, you can also use the KeyPress event for the same purpose: Private Sub DataGrid1_KeyPress(KeyAscii As Integer) If KeyAscii = vbKeyReturn Then KeyAscii = vbKeyTab End If End Sub 

Additional query words: DataGrid

Keywords: kbhowto kbdatabinding kbctrl KB291191

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.