Scroll bar in wrong position - DataGridView

1

I'm trying to select the last row in a DataGridView after adding a new item. For this I am executing the following code:

dgwVenda.ClearSelection()
dgwVenda.Rows(dgwVenda.Rows.Count - 1).Selected = True
If dgwVenda.Controls(1).Visible = True Then
    dgwVenda.FirstDisplayedScrollingRowIndex = dgwVenda.Rows(dgwVenda.Rows.Count - 1).Index
End If

After execution the last line is selected and appearing in the Grid, however the scrollbar (vertical) is at the top when it should be at the bottom. Also scrolling the mouse does not work.

After the position of the bar is changed by a click of the mouse its operation returns to correct.

Is there any way for the scroll bar to stay in the down position and the scrolling work by scrolling the mouse?

    
asked by anonymous 21.06.2018 / 20:23

1 answer

1

If the goal is to put Scroll always positioned on the last line, you can use the following code:

dgwVenda.FirstDisplayedScrollingRowIndex = dgwVenda.RowCount - 1

As for the Scroll of DataGridView does not work with the scroll of the mouse, this may be due to the grid losing focus and thus the possibility of using Scroll . If so, you can "force" the focus by moving the mouse on the grid:

Private Sub dgwVenda_MouseEnter(sender As Object, e As EventArgs) Handles dgwVenda.MouseEnter
    dgwVenda.Focus()
End Sub
    
05.07.2018 / 14:13