Click here to Skip to main content
15,895,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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 :

VB
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:
VB
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
Updated 11-Feb-13 10:34am
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)



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