Datagridview text for vertical scrollbar display

si2030 used Ask the Experts™
Hi Experts

I have datagridview that is bound to a datasource. It has 9 columns. I need to know when the vertical scroll bar is displayed and be able to automatically adjust the position of textbox for the width of the scroll bar?

The user could reduce the size of the height of the datagridview prompting the DGV to automatically show the vertical scrollbar. if this happens the textbox will cease to be in line with one of the columns. I need to move it by the amount of the vertical scrollbar. So...

I need to fire a subroutine as soon as the vertical srollbar displays and also when it ceases to display...

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®


This cant be that hard?
The code below will run a method based on the visibility of the scrollbar
    Dim ScrollBarTB As New TextBox
    Private Sub DataGridView1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint
        Debug.Print("Listing Controls")
        'List the child controls of the DatagridView'
        For Each Control As Control In DataGridView1.Controls
            'Find the VScrollBar'
            If Control.GetType.Name = "VScrollBar" Then
                Debug.Print(vbTab & Control.Visible)
                If Control.Visible Then
                End If
                'The below code will hide and show a textbox based on the visibility of the VScrollBar'
                ScrollBarTB.Visible = Control.Visible
                If Not DataGridView1.Parent.Controls.Contains(ScrollBarTB) Then
                    Me.Controls.SetChildIndex(ScrollBarTB, 0)
                End If
                ScrollBarTB.Location = New Drawing.Point(0, 0)
                ScrollBarTB.Height = 50
                ScrollBarTB.Width = 100
            End If
        Debug.Print("Finished listing controls")
    End Sub

Open in new window

For example...
DataGridView1.FirstDisplayedScrollingRowIndex = Me.DataGridView1.RowCount - 2

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial