scrolling in datagridview

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
I would like to know when I use the up/down arrows from the scrollbar, the selected row disappears, instead of scrolling along the datagridview.

I have seen some codes when searching through google, but those I found are scrolling directly to the last row or to specific row.
All I want is just to scroll along the datagridview when using the up/down arrows.

Thanks.

BTW: when I use the arrow keys I can scroll the selected row.
 
Last edited:
when I use the up/down arrows from the scrollbar, the selected row disappears, instead of scrolling along the datagridview.
Not sure if I misunderstand you, but when I use the scrollbar the selected row remains selected.
 
Not sure if I misunderstand you, but when I use the scrollbar the selected row remains selected.

That happends with me aswell (the selected row scrolls out of view), but I would like the full row to scroll along. When I use the arrow keys on my keyboard I can scroll through the datagridview.
 
but I would like the full row to scroll along.
What do you mean? The selected row scrolls as it should.
 
What do you mean? The selected row scrolls as it should.

Well it doesn't otherwise I wouldn't be asking this question.

When I have selected a row in the grid, I use the down arrow of the scrollbar to scroll through the grid (as when it's selection changes, cell info is being put into textboxes, when I scroll), but now the selected row moves out of view, it doesn't scroll along.
So there must be something wrong, if it does with you.
 
So, are you saying that, as you scroll, you want the selection in the grid to change? As in, if you scroll down the row below the selected row gets selected, then the row below that, etc? If so then you're going to have to define rules. It's all very well saying that you want the selected row to "scroll along" but what EXACTLY does that mean?
 
If so then you're going to have to define rules.

I have tried some samples, but they didn't work, some just selected more row while scroll down.

It's all very well saying that you want the selected row to "scroll along" but what EXACTLY does that mean?

What I mean with "scroll along", is not that the selected row should "move up/down", but just the selection of a row should scroll up or down according to the scrollbar.

Hope this makes it more clear.
 
Okay, I've got a sample here:

VB.NET:
Private Sub DataGridViewX1_Scroll(sender As Object, e As System.Windows.Forms.ScrollEventArgs) Handles DataGridViewX1.Scroll
        Dim rc As DataGridViewSelectedRowCollection = DataGridViewX1.SelectedRows

        If e.ScrollOrientation = ScrollOrientation.VerticalScroll Then
            If e.NewValue > e.OldValue AndAlso rc.Count > 0 Then
                Dim nextrow As Integer = rc(0).Index + 1
                DataGridViewX1.Rows(nextrow).Selected = True
            End If
        End If
    End Sub

What this code does, it does scroll the selection. But it adds a row to the selection (mutiple rows) when scrolling with the down arrow of the scrollbar, I just want a single row to be selected.

Also when I use the scrollbars up arrow, the selection is changing, all I see are the rows selected (multiple).
 
Well, that's what I guessed you meant but do you see where I emphasised the word EXACTLY? That implies specific details of how it should work. Programming requires absolutes, not generalities.
 
Well, that's what I guessed you meant but do you see where I emphasised the word EXACTLY? That implies specific details of how it should work. Programming requires absolutes, not generalities.

You posted just a minute after mine, but lets explain EXACTLY:

row1
row2 <-- selected
row3
row4
row5

After using the scrollbar down arrow:

row1
row2
row3 <-- selected
row4
row5

After using the scrollbar up arrow:

row1
row2 <-- selected
row3
row4
row5

Only one row selected, no mutiple.

Hope this explain exactly how it should work.

Thanks
 
If you wanted to select a different row when scrolling you could have just said that, of course including the information on how you want the selection to take place.
I just want a single row to be selected.
Select 'False' for MultiSelect property.
 
Select 'False' for MultiSelect property.

Strange I thought I already had. Well that solves the multi select issue.

Only thing remains is the up scrolling, that doesn't work, down works.

I have been trying the following:
VB.NET:
Dim rc As DataGridViewSelectedRowCollection = DataGridViewX1.SelectedRows

        If e.ScrollOrientation = ScrollOrientation.VerticalScroll Then
            If e.NewValue > e.OldValue AndAlso rc.Count > 0 Then
                Dim nextrow As Integer = rc(0).Index + 1
                DataGridViewX1.Rows(nextrow).Selected = True
            End If
           [COLOR="#FF0000"] If e.NewValue > e.OldValue AndAlso rc.Count - 1 Then
                Dim prevrow As Integer = rc(0).Index - 1
                DataGridViewX1.Rows(prevrow).Selected = True
            End If[/COLOR]
        End If

Red should be the code for scrolling up.
 
It can't be down and up at the same time, either it is up ELSE it is down. One condition will determine which direction it is.
 
Back
Top