none
mouse wheel on datagridview and scroll bar RRS feed

  • Question

  • 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


    Wednesday, March 28, 2018 5:10 PM

Answers

  •  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

    • Marked as answer by Claudio111 Thursday, March 29, 2018 7:33 AM
    Wednesday, March 28, 2018 9:21 PM

All replies

  •  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

    • Marked as answer by Claudio111 Thursday, March 29, 2018 7:33 AM
    Wednesday, March 28, 2018 9:21 PM
  • Perfect.

    Thank you Iron

    Thursday, March 29, 2018 7:33 AM