Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scroll datagrid using Scrollbar control

Status
Not open for further replies.

ErnstJan

Programmer
Nov 10, 2002
114
0
0
NL
Some info up front.

Have a panel(pnlMain) containing a datagridview and an other panel (pnlFilter).
pnlFilter contains textboxes to filter the data in the datagridview.
Also the width of the textboxes are synchronized with the width of the colums of the datagridview.
To keep the textboxes lined up with the column I make the width of datagridview the same as the total width of the columns.
By doing so the vertical scroll bar of the datagridview can move out of view.
To prevent that users have to move the horizontal scrollbar to get to the vertical scrollbar. I placed a vertical scrollbar control.
I managed to set it to the settings of the vertical scrollbar of the datagrid view.
Also I can move the vscrollbar control when the scrollwheel is used on the datagridview.
But I can't get the datagridview to scroll when using the vscrollbar control.

I know that you can use FirstDisplayedScrollingRowIndex to scroll to a row, but I can't use the value of the scrollbar control for it because it's telling my that its out of range.

All other ideas on solving this problem is most welcome.

Thx in advance.

Greetings
Ernst Jan
 
Try
1. Changing the Vertical Scrollbar control's LargeChange property to 1.
2. In Vertical Scrollbar control's Scroll event:
DataGridView1.CurrentCell = DataGridView1.Item(0, e.NewValue)

Hope this helps.
 
Mansii,

Thank you for your response.
Unfortunally it's a partial solution.
I can now scroll with the scroll bar but bar is not showing the way the orginal scrollbar would show it.

What I am trying to do is to mimic the vertical scrollbar of the datagridview.

Code:
            'Scroll balk aanmaken
            With DirectCast(dgvOverzicht.Controls(1), Windows.Forms.ScrollBar)
                'Controleren of scrollbar zichtbaar is
                If .Visible = True Then
                    'gegevens overnemen
                    dgvScrollBar.Maximum = .Maximum
                    dgvScrollBar.Minimum = .Minimum
                    dgvScrollBar.SmallChange = .SmallChange
                    dgvScrollBar.LargeChange = .LargeChange
                Else
                    dgvScrollBar.Maximum = 1
                    dgvScrollBar.Minimum = 1
                End If
            End With

Code above I use to setup my scrollbar.
In the datagridview scroll event I placed this

Code:
    Private Sub dgvOverzicht_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles dgvOverzicht.Scroll


        'Scroll balk aanmaken
        dgvScrollBar.Value = DirectCast(dgvOverzicht.Controls(1), Windows.Forms.ScrollBar).Value

    End Sub

This works fine. The custom bar is nicely following the bar of the datagridview.
I tried the reversed code in the custom scroll event but then the datagrid is not responding. It's not moving up and down.

Hope it's more clear now. And maybe you or someone else can tell how to get it to work.

Greetings,
Ernst Jan





 
Found a solution for my problem.
It's not perfect but it's working.

The scrollbar has a maximum value.
That value I divided by the total number of rows and place it in a variable.
In the scroll event of the custom scroll bar I placed this

Code:
    Private Sub dgvScrollBar_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles dgvScrollBar.Scroll

        DirectCast(dgvOverzicht.Controls(1), Windows.Forms.ScrollBar).Value = e.NewValue
        If e.NewValue / varDivider >= dgvOverzicht.RowCount - 1 Then
            dgvOverzicht.FirstDisplayedScrollingRowIndex = dgvOverzicht.RowCount - 1
        Else
            dgvOverzicht.FirstDisplayedScrollingRowIndex = e.NewValue / varDivider
        End If

    End Sub

Found out that when the datagrid is very small the movement of the scrollbar is not good.


 
Sorry I didn't get the question correctly. But glad to hear that you got the solution.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top