How to change the line selection of the datagridview according to the vertical scrollbar?

Nexw 1 Reputation point

I am working on a project.
Performing a search in the database and throwing the result into the datagridview (dgv), I can get many lines. I need that as I use the vertical scroll bar to down or up automatically change the selection of the line according to the position of the vertical scroll bar.

I tried to use the code below:

Private Sub dgvdados_Scroll(sender As Object, e As ScrollEventArgs) Handles dgvdados.Scroll

 dgvdados.Rows(e.NewValue).Selected = True
End Sub

However, without success, is there any way to achieve the result I want?

An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,496 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,011 Reputation points

    Use a BindingSource. Instead of setting the DataSource to whatever it is now set it to the BindingSource. Then set the DataSource of the DataGridView to the BindingSource.

    In the scroll event

    Private Sub DataGridView1_Scroll(sender As Object, e As ScrollEventArgs) Handles DataGridView1.Scroll  
        customersBindingSource.Position = e.NewValue  
    End Sub  

    BindingSource is privately scoped

    Public Class Form1  
        Private customersBindingSource As New BindingSource  

    Now if not using a BindingSource you should.

  2. Xingyu Zhao-MSFT 5,351 Reputation points

    Hi @Nexw ,
    I set the DataSource of DataGridView to a datatable.
    Here's the whole code of my test.

    Private verticalScrollingOffset As Integer = 0  
    Private lastOffset As Integer = 0  
    Private oldValue As Integer = 0  
    Private index As Integer  
    Private Sub dgvdados_Scroll(sender As Object, e As ScrollEventArgs) Handles dgvdados.Scroll  
        verticalScrollingOffset = dgvdados.VerticalScrollingOffset  
        Dim valueChange = e.NewValue - oldValue  
        If Not verticalScrollingOffset = lastOffset Then  
            If Math.Abs(valueChange) < dgvdados.Rows.Count Then  
                Dim RowIndex As Integer = dgvdados.CurrentRow.Index + valueChange  
                dgvdados.CurrentCell = dgvdados.Item(index, RowIndex)  
                If RowIndex < dgvdados.Rows.Count Then  
                    dgvdados(index, RowIndex).Selected = True  
                End If  
            End If  
        End If  
        lastOffset = verticalScrollingOffset  
        oldValue = e.NewValue  
    End Sub  
    Private Sub dgvdados_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvdados.CellClick  
        index = dgvdados.CurrentCell.ColumnIndex  
    End Sub  

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.