Results 1 to 5 of 5

Thread: [RESOLVED] DGV Row events() don't work on the last DGV row.

  1. #1

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    288

    Resolved [RESOLVED] DGV Row events() don't work on the last DGV row.

    Hello!

    I'm using WindowsForms app with VB.NET.

    I have a DGV with a couple of columns and only one column is editable (Column X).
    When I open my Form, the focus goes straight to the first cell on Column X so I can change it's value and when if I press ENTER, it multiplies some cells and update other cells values, then it passes the focus to the next row on Column X and so on, so far everything seems to work fine, the problem comes when it reaches the last row on Column X, if I press ENTER and nothing happens, I've already tried different DGV events() like:[CellValueChange(), RowLeave(), RowValidating() etc...] but still nothing happens when the focus is on the last row of Column X.
    In order for this to work, I have realized that if I leave the cell, I mean if I move one cell up, then the last row does its thing. but to me it feels unnatural moving up or down in order for things to work.

    I have no idea how to overcome this. any advice?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,738

    Re: DGV Row events() don't work on the last DGV row.

    Pressing Enter will move you down one row, which means the focus shifts and that kicks off events. If you're on the last row then you can't move down a row so there's no focus shift so there's no events, so there's no event handlers. Handing the CellValueChanged event is useless if there's no event raised, and there will be no event unless the focus shifts or you explicitly end the current editing session. What you could do is trap the Enter key, check whether you're on the last row and, if so, explicitly end the editing session. If your data is bound, use a BindingSource and call its EndEdit method, otherwise call EndEdit on the grid.

  3. #3

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    288

    Re: DGV Row events() don't work on the last DGV row.

    OK...
    I've tried this, and still not working...

    VB.NET Code:
    1. Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown
    2.         Dim My_Current_Cell As Integer
    3.         My_Current_Cell = DataGridView1.Rows.Count - 1
    4.         If e.KeyCode = Keys.Enter Then
    5.             If DataGridView1.CurrentCell.RowIndex = My_Current_Cell Then
    6.                 DataGridView1.EndEdit()
    7.             End If
    8.         End If
    9.     End Sub

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,738

    Re: DGV Row events() don't work on the last DGV row.

    ALWAYS assume that "not working" is not an adequate explanation of anything. What actually did happen? Did you put a breakpoint on that method to see whether it was even executed? If not then why are we back here yet?

  5. #5

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    288

    Re: DGV Row events() don't work on the last DGV row.

    Yes I put a breaking Point on the DataGridView1_KeyDown() event, and it doesn't even stops there, so I change this DGV property:
    vb.net Code:
    1. DataGridView1.EditMode = DataGridViewEditMode.EditOnKeystroke
    and now it works, even when the focus is on the last row.

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