2014-12-18 66 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