guys i have an issue with scrolling a datagridview (VB.Net 2010) using the mouse wheel
i created a mousewheel event an i am catching the mouse-wheel movement up or down
i created a scroll event and set the boundaries for the scroll, but i have no idea how to combine both procedures:

Mouse wheel event

Public Sub grdMouseWheel(ByVal sender As Object, ByVal e As MouseEventArgs)
        Dim RowIndex As Integer = grdAccountTypes.CurrentRow.Index
        If e.Delta < 0 Then
            MessageBox.Show("Mouse wheel is being moved down")
        Else
            'RowIndex = grdAccountTypes.CurrentRow.Index + 1
            'RowIndex = grdAccountTypes.FirstDisplayedScrollingRowIndex Then
            ' grdAccountTypes(0, 0).Selected = True
            MessageBox.Show("Mouse wheel is being moved up")
        End If

Scroll Event

Public Sub grdScroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs)
        Dim RowIndex As Integer = grdAccountTypes.CurrentRow.Index + e.NewValue
        If grdAccountTypes.RowCount = 0 Then  ' if no row exist right now
            MessageBox.Show("No row existed right now. Please add some records")
        ElseIf RowIndex = grdAccountTypes.Rows.Count() - 1 Then
            grdAccountTypes(0, 0).Selected = True
        Else
            grdAccountTypes(0, RowIndex).Selected = True
        End If

    End Sub

any help would be appreciated

Recommended Answers

All 3 Replies

You need to split the functionality in grdScroll like:

Public Sub grdScroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs)
        Dim RowIndex As Integer = grdAccountTypes.CurrentRow.Index + e.NewValue
        SelectNewRow(RowIndex)
    End Sub

Public Sub SelectNewRow(Byval RowIndex as Integer)
    If grdAccountTypes.RowCount = 0 Then  ' if no row exist right now
            MessageBox.Show("No row existed right now. Please add some records")
    Else
        If RowIndex >= grdAccountTypes.Rows.Count() - 1 Then
            grdAccountTypes(0, 0).Selected = True
        ElseIf RowIndes < 0 then
            grdAccountTypes(0, grdAccountTypes.Rows.Count() - 1).Selected = True           
        Else
            grdAccountTypes(0, RowIndex).Selected = True
        End If
    End If
End Sub

Then you can modify the grdMouseWheel to act as:

Dim RowIndex As Integer = grdAccountTypes.CurrentRow.Index
        If e.Delta < 0 Then
            RowIndex -=1
        ElseIf e.delta > 0
            RowIndex +=1
        End If
        SelectNewRow(RowIndex)

Hope this helps

it actually works kind of,
i have one more question for you, i would like to stop the dgv from scrolling in both end,
so i did modify the Select new row event and i can stop the scroll at the bottom.
what i don't understand how to do is to stop the grid from circling at the top

Public Sub SelectNewRow(ByVal RowIndex As Integer)
        Dim LastRow As Integer
        LastRow = grdAccountTypes.Rows.Count() - 1

        If grdAccountTypes.RowCount = 0 Then ' if no row exist right now
            MessageBox.Show("No row existed right now. Please add some records")
        Else
            If RowIndex >= grdAccountTypes.Rows.Count() - 1 Then
                grdAccountTypes(0, LastRow).Selected = True
            ElseIf RowIndex < 0 Then
                grdAccountTypes(0, grdAccountTypes.Rows.Count() - 1).Selected = True
            Else
                grdAccountTypes(0, RowIndex).Selected = True
            End If
        End If
    End Sub

Instead of

grdAccountTypes(0, grdAccountTypes.Rows.Count() - 1).Selected = True

try

grdAccountTypes(0, 0).Selected = True


Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.