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

Nexw 1 Reputation point
2021-05-10T19:03:57.9+00:00

I am working on a vb.net 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?

Developer technologies VB
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-05-10T20:11:22.89+00:00

    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,381 Reputation points
    2021-05-12T01:23:34.073+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.