0
votes

I have a datagridview where I want to scroll it from a separate horizontal scroll bar. I.e. When I move this bar, it scrolls datagridview with it.

Here's what I have so far:

Private Sub HScrollBar1_Scroll(sender As Object, e As ScrollEventArgs) Handles HScrollBar1.Scroll
    cameraTable.HorizontalScrollingOffset = e.NewValue
End Sub

The datagridview doesn't scroll though. Any suggestions?

1
Found the problem: The datagridview didn't have enough columns yet to scroll across. - Kat

1 Answers

0
votes

Use the DataGridView.CurrentCell or DataGridView.FirstDisplayedCell property to scroll the display.

Option Strict On

Public Class Form1

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    'Fill grid with dummy data
    Dim dtb As New DataTable("MyDataTable")
    For i As Integer = 0 To 99
      dtb.Columns.Add("C" & i.ToString)
    Next i
    For j As Integer = 0 To 99
      Dim s(99) As String
      For i As Integer = 0 To 99
        s(i) = ((i + j) Mod 100).ToString
      Next i
      dtb.Rows.Add(s)
    Next j
    DataGridView1.DataSource = dtb
  End Sub

  Private Sub HScrollBar1_Scroll(sender As Object, e As ScrollEventArgs) Handles HScrollBar1.Scroll
    'Use the DataGridView.FirstDisplayedCell property to scroll the display
    DataGridView1.FirstDisplayedCell = DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(e.NewValue)
  End Sub
End Class