שתף באמצעות


mouse wheel on datagridview and scroll bar

Question

Wednesday, March 28, 2018 5:10 PM

HI

I have a DataGriView bound to BindingSource (BS1) bound to DataTable with hundred of rows

I want to use the mouse wheel to change the selected row on DGV

I use this code

    
' On datagridview Load Event.....
AddHandler Me.MouseWheel, AddressOf DGV_MouseWheel
.....
    
Private Sub DGV_MouseWheel(sender As Object, e As MouseEventArgs) Handles DGV.MouseWheel
    If e.Delta > 0 Then
        BS1.Position = BS1.Position - 1
    Else
        BS1.Position = BS1.Position + 1
    End If
End Sub

The problem is that the selected row change correctly with the mouse wheel but also the scrollbar scrolls one position every time

I would like the scrollbar to scroll only when the Selected row goes out of the actual displayed rows, but not when the selected row is inside the actual view.

Any Suggestion ?

Thanks

Claudio

All replies (2)

Wednesday, March 28, 2018 9:21 PM ✅Answered

 You can cast the MouseEventArgs class that is passed to the MouseWheel event,  to a HandledMouseEventArgs Class. and set the Handled property to True.  Try changing the MouseWheel event as shown below.

    Private Sub DGV_MouseWheel(sender As Object, e As MouseEventArgs) Handles DGV.MouseWheel
        CType(e, HandledMouseEventArgs).Handled = True
        If e.Delta > 0 Then
            BS1.Position = BS1.Position - 1
        Else
            BS1.Position = BS1.Position + 1
        End If
    End Sub

 

 Here you can see it will not allow the datagridview to scroll with the mouse wheel unless the selected row is at the top or bottom and is scrolled further.

If you say it can`t be done then i`ll try it


Thursday, March 29, 2018 7:33 AM

Perfect.

Thank you Iron