question

Nexw-7052 avatar image
0 Votes"
Nexw-7052 asked Nexw-7052 commented

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

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?

dotnet-visual-basic
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Nexw-7052 ,
It seems that you asked the same question on stack overflow forum before, but later deleted it.
Here's the result of my test:
95410-gif.gif
If you need further assistance, please provide more details here.
We are waiting for your update.

1 Vote 1 ·
gif.gif (144.4 KiB)

@XingyuZhao-MSFT, yes, exclude it so that I could reformulate the question in an understandable way for more people to help me, but I reached the limit of questions and then I came here. and really in this gif that you posted is exactly what I need. could you provide me with the test code please?

0 Votes 0 ·

Your code seems to work, i.e. the top visible row is selected, however you must also unselect the previous rows using ‘dgvdados.ClearSelection()’ or set Multiselect property to False in Form Designer.


0 Votes 0 ·

even with dgv.clearselection (), it still doesn't work the way I need it. the comment previous to yours showing the gif is what I need.

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered Nexw-7052 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the feedback, but I don't have a DataSource, the result of the database directly included in the dgv. as i'm new to programming, i'll look for how to add bindingsource to dgv and test your solution, ask for some time for testing and return.

0 Votes 0 ·

I was able to create and test his resolution example, it works, however it doesn't matter if I move it down or up, it selects only the first line of the dgv.

0 Votes 0 ·
XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered Nexw-7052 commented

Hi @Nexw-7052 ,
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.


· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@XingyuZhao-MSFT , this really works, but it is still not the result I hope for, but it helps me a lot very grateful for the return, the result I want is like this:

2

I don't know if you'll be able to access the link, it's a gif of how I really wanted it to look. your example was the one that came closest.


0 Votes 0 ·

Hi @Nexw-7052 ,
I can't see the GIF.
Make sure your gif is 3.1MB or smaller.
95940-2.png


0 Votes 0 ·
2.png (19.9 KiB)

Hi, @XingyuZhao-MSFT

Image with the code you posted for help:
95999-code-mic.png

In the gif below, it shows the code working when I use the vertical scrollbars down and up.

96000-ezgifcom-gif-maker.gif

Using his code, it generates an error when I throw the horizontal bar to the side and try to change the line with the scroll

95915-error.gif

Result I want:

96018-resultado-que-almejo.gif


0 Votes 0 ·
code-mic.png (16.4 KiB)
error.gif (2.7 MiB)
Show more comments

Hi, @XingyuZhao-MSFT
in the case when starting the search and having the data filled in the dgv, I use full row select. as i will not have any cell selected, and i move the horizontal and then vertical scroll down it still generates the error. because I believe that it is because I need to have at least one cell selected for it to have the index column

96197-6b4d1d37355bc95dc1c7b1fca278a5b8.gif


0 Votes 0 ·