Click here to Skip to main content
12,508,630 members (37,127 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET DataGridView
Hi,

I am using visualstudio 2005, with a datagridview that has 1 row and 7 columns.

I am trying to make it so that when the user enters data in the last cell and hits enter that the data is committed by exiting the cell, but the datagridview is not recognising that the enter key is being pressed.

The datagridview is not bound to a datatable, and the datatable is updated by the user pressing a button.

I have the following handler written for editcontrolshowing:

If Me.DataGridView1.CurrentCell.ColumnIndex = 6 Then
  Dim tb As TextBox = CType(e.Control, TextBox)
  AddHandler tb.KeyPress, AddressOf DataGridView1_KeyPress
End If

This is to use e.handled in keypress to restrict entry to numbers, decimal point etc..

then under keypress before the e.handled line :

If Me.DataGridView1.CurrentCell.ColumnIndex = 6 AndAlso e.KeyChar = _
          Convert.ToChar(Keys.Enter) Then
Me.DataGridView1.CurrentCell = Me.DataGridView1(0, 0)
End If

The keypress is not recognising that the enter key is being pressed. If I change the key to a number or letter then it does recognise the keypress and moves to cell Me.DataGridView1(0, 0).

I have tried the following, but it still does not work:

Capturing the enter key through keydown with e.KeyCode
Removing all aother code in keypress, other than the enter capture
Enabling Keypreview on the form
Coding acceptreturn = true
Changing the cell from edit on enter to edit on F2
Using return instead of enter
Using the ascii character
Changing control focus

I have noted that is does not recognise the tab key either, but this is not a problem because the tabkey exits the cell and commits the data.

I have tried:
If e.KeyCode = Keys.Return AndAlso Me.DataGridView1.CurrentCell.ColumnIndex = 6 Then
        SendKeys.SendSend("{TAB}")
End If

This does not work, again because the return key press is not recognised.

Does any one have any ideas please?

Thank you,

Peter.
Posted 11-Feb-13 9:24am
Updated 11-Feb-13 10:34am
CHill60158.1K
v5
Comments
joshrduncan2012 11-Feb-13 15:26pm
   
If memory serves me correctly, the 6th column has a column index of 5 since the column indexes start with 0 (same with rows).
PeterScaletti 11-Feb-13 15:35pm
   
Hi Josh, thank you for your reply. My mistake, the datagridview has 7 columns. I have updated my question

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160927.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100