2014-12-18 131 views
0

我有一個datagridview我想從一個單獨的水平滾動條滾動它。即當我移動這個欄時,它會用它滾動datagridview。從不同的滾動條滾動datagridview?

這是我到目前爲止有:

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

在DataGridView並不雖然滾動。有什麼建議麼?

+1

它爲我滾動。 – LarsTech

+0

發現問題:datagridview沒有足夠的列尚未滾動。 – Kat

回答

0

使用DataGridView.CurrentCellDataGridView.FirstDisplayedCell屬性滾動顯示。

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