我有一个DataGridView
,我不断填充添加行BackgroundWorker
。窗体:禁用DataGridView上的自动滚动
问题是,即使在DataGridView
的底部添加了每一行,滚动条仍继续向上移动,而不是在我之前移动它的相同位置上保持 。
我怎样才能禁用自动滚动保持DataGridView
在同一地点,而新的行添加在底部?
我有一个DataGridView
,我不断填充添加行BackgroundWorker
。窗体:禁用DataGridView上的自动滚动
问题是,即使在DataGridView
的底部添加了每一行,滚动条仍继续向上移动,而不是在我之前移动它的相同位置上保持 。
我怎样才能禁用自动滚动保持DataGridView
在同一地点,而新的行添加在底部?
我通常这样做有ListView
,我相信一个DataGridView
将同样的工作:
If ListView1.Items.Count > 0 Then
ListView1.Items.Item(ListView1.Items.Count -1).EnsureVisible()
End If
这将确保您的最后一个条目是可见的。
您可以使用DataGridView1_Scroll获取特定滚动位置的事件。像下面 -
Dim savedScrollPosition As Integer = 0
Private Sub DataGridView1_Scroll(sender As Object, e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView1.Scroll
savedScrollPosition = e.NewValue
End Sub
然后你就可以添加新行后分配这DataGridView的财产FirstDisplayedScrollingRowIndex。像下面 -
Public Class Form1
Dim i As Integer = 1
Dim savedScrollPosition As Integer = 0
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim timer As New Timer()
timer.Interval = 1000
AddHandler timer.Tick, AddressOf timer_Tick
timer.Start()
End Sub
Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
UpdateDGV()
i = i + 1
End Sub
Private Sub UpdateDGV()
DataGridView1.ColumnCount = 2
DataGridView1.Columns(0).Name = "Product_Name"
DataGridView1.Columns(1).Name = "Product_Price"
Dim row As String() = New String() {"Product # " + Convert.ToString(i), 1000 + i}
DataGridView1.Rows.Add(row)
'Keep your scrollbar at specific position
DataGridView1.FirstDisplayedScrollingRowIndex = savedScrollPosition
'keep your scrollbar at bottom of the grid
'DataGridView1.FirstDisplayedScrollingRowIndex = DataGridView1.RowCount - 1
End Sub
Private Sub DataGridView1_Scroll(sender As Object, e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView1.Scroll
savedScrollPosition = e.NewValue
End Sub
End Class
这个问题以前有人问及有答案:[保存和重置第一行显示(http://stackoverflow.com/a/39935694/6664878)或[覆盖Rowchanged事件](HTTP ://stackoverflow.com/a/1521507/6664878)...你有没有尝试过这些? – soohoonigan