Results 1 to 5 of 5

Thread: [RESOLVED] DataGridView KeyDown/KeyPress to return new cell value

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Resolved [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width