我有一个DataGridView
我绑定到 aBindingSource
到 aDataView
到 a DataTable
。当将行添加到时,DataTable
我希望更改DataGridView
自动出现,就像他们应该的那样。标准的东西。
如果我将DataTable
实例放在与 相同的表单上DataGridView
,则会触发BindingSource
'ListChanged
事件并DataGridView
刷新。但是如果我把它DataTable
放在其他任何地方,比如在一个模块中或另一个表单上,除非我最小化并恢复它的表单,否则ListChanged
事件不会触发并且DataGridView
不会自动刷新。为什么?
更新
这是我如何在一个简单的测试应用程序中执行此操作的示例:
模块:
Imports System.ComponentModel
Module modTick
Private WithEvents tmr As System.Timers.Timer
Private r As New Random()
Public dt As DataTable
Public Sub Init()
dt = New DataTable("table")
dt.Columns.Add("Number")
dt.Rows.Add(New Object() {r.Next})
dt.Rows.Add(New Object() {r.Next})
dt.Rows.Add(New Object() {r.Next})
End Sub
Public Sub StartTimer()
tmr = New System.Timers.Timer()
tmr.AutoReset = True
tmr.Interval = 500
tmr.Enabled = True
End Sub
Private Sub _tmrPulse_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tmr.Elapsed
dt.Rows.Add(New Object() {r.Next})
End Sub
End Module
表格:
Public Class Form1
Private dv As DataView
Private WithEvents bs As BindingSource = New BindingSource()
Private WithEvents tmr As Timer = New Timer()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
modTick.Init()
dv = New DataView()
dv.Table = modTick.dt
dv.AllowDelete = False
dv.AllowEdit = False
dv.AllowNew = False
dgdisplay.AutoGenerateColumns = True
bs.DataSource = dv
dgdisplay.DataSource = bs
tmr.Interval = 500
tmr.Enabled = True
modTick.StartTimer()
End Sub
Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
Label1.Text = bs.Count
End Sub
End Class
因此,如果我将模块的代码放入表单中,则会因为'事件触发而DataGridView
刷新。照原样,它既不触发也不刷新。但是如果我最小化并恢复表单,更改就在那里。显示了项目的数量,这是正确的。BindingSource
ListChanged
Label
DataTable