[RESOLVED] DataGridView KeyDown/KeyPress to return new cell value
Hi,
There are a few threads to show how to handle the keypress event but I can't find a way to update the datagridview cell with the new contents of the editing textbox control. If I add a character to a the text of a cell I want it to react with the new cell contents. Is this possible?
Code:
Private Sub DGV_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DGV.EditingControlShowing
If PauseEvents = False Then
Dim txtEdit As System.Windows.Forms.TextBox = e.Control
'remove any existing handler
RemoveHandler txtEdit.KeyPress, AddressOf txtEdit_Keypress
AddHandler txtEdit.KeyPress, AddressOf txtEdit_Keypress
End If
End Sub
Private Sub txtEdit_Keypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
'Need to update the datagridview somehow
End Sub
Re: DataGridView KeyDown/KeyPress to return new cell value
Why would you need to do that? The DataGridView is specifically designed to update the cell Value when the editing session ends.
Re: DataGridView KeyDown/KeyPress to return new cell value
Hi jmcilhinney,
The datagridview is shown a drafting program (AutoCAD) where the user is able to click away from the datagrid into the drafting area. It doesn't seem to losefocus when this happens, I guess because it hasn't lost focus in the container so doesn't finalize the input back into the datagrid.
I was hoping that I can make updates on each keypress.
Re: DataGridView KeyDown/KeyPress to return new cell value
What exactly is this drafting area? Is it a control that can't actually receive focus? The reason that you have to handle the EditingControlShowing event is that there is no control in the cell to begin with and only when you start editing is one embedded. That control is a regular TextBox so, just like any other TextBox, if you shift focus to another control then it will lose focus. In the case of a DataGridView editing control, as soon as it loses focus the editing session ends, the control is removed and the appropriate property value assigned to the Value of the cell. If you want to force the data to be pushed from the editing control to the cell's Value then you should end the editing session, which you can do by calling EndEdit.
Re: DataGridView KeyDown/KeyPress to return new cell value
Thanks for help jmcilhinney.
I figured out a way to do it using one of the application's reactors and the EndEdit you suggested.