Vb.net ListChanged事件未触发?

Vb.net ListChanged事件未触发?,vb.net,data-binding,datagridview,bindinglist,listchanged,Vb.net,Data Binding,Datagridview,Bindinglist,Listchanged,我有一个类,其中有一个bindingslist(of T)。bindinglist绑定到我表单上的datagridview。将项添加到绑定列表时,它们会显示在数据网格视图中,但是滚动条不会更改以适应新数据。我开始认为这是因为Listchanged事件没有被触发(或者没有被我的表单正确捕获)。我的代码设置如下: 数据类: Public Class data Implements INotifyPropertyChanged Public Sub new(byVal att1 as

我有一个类,其中有一个
bindingslist(of T)
bindinglist
绑定到我表单上的
datagridview
。将项添加到
绑定列表
时,它们会显示在
数据网格视图
中,但是
滚动条
不会更改以适应新数据。我开始认为这是因为
Listchanged
事件没有被触发(或者没有被我的表单正确捕获)。我的代码设置如下:

数据类:

Public Class data

    Implements INotifyPropertyChanged
    Public Sub new(byVal att1 as string, ByVal att2 as string)
       Attribute1 = att1
       Attribute2 = att2

    End sub

    Private mAttribute1 as string
    Public Property Attribute1 as string
        Get
            return mAttribute1
        End get
        Set(ByVal value as string)
            mAttribute1 = value
            OnPropertyChanged("Attribute1")
        End Set
    End Property
    Private mAttribute2 as string
    Public Property Attribute2 as string
        Get
            return mAttribute2
        End Get
        Set(ByVal value as string)
            mAttribute2 = value
            OnPropertyChanged("Attribute2")
        End Set
    End Property

    Public Sub OnPropertyChanged(ByVal name As String)
         RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
    End Sub

    Public Sub ChangeDataFormat()
        'change from one format to the other
    End Sub
End Class
数据生成器类:

Public Class dataGenerator()
    private myThread as New System.Theading.Thread(address of StartDataGeneration)
    Public Sub new()
        mDataList = new bindingList(of Data)
        mDataList.RaiseListChangedEvents = True
    Private WithEvents mDataList as bindingList(Of Data)
    Public readonly DataList as bindingList(of Data)
        Get
            Return mDataList
        End Get
    End property
    Private Sub StartDataGeneration()
        dim att1 as integer = 1
        dim att2 as integer = 2
        for i as Integer = 0 to 1000
            mDataList.Insert(0,New Data(att1.ToString,att2.ToString)
            att1 *= 2
            att2 *=3
        next
    End Sub
    Public Sub StartDataThread()
        myThread.Start()
    End Sub
    Public Sub ChangeDataFormat()
        for each d as data in mDataList
            d.ChangeDataFormat()
        next
    End Sub
End Class
表格:

我知道有很多代码,但我想说清楚。因此,当我单击开始按钮时,新项目开始出现,但是,一旦它们到达网格底部,滚动条就不会出现。如果单击“更改格式”按钮,数据将正确更改格式并更新网格中的数据。我的印象是
ListChanged
事件将自动与
bindinglist
datagridview
一起工作。我尝试在
myDataGridView
上调用update和refresh,并将
datagridview.datasource
设置为nothing,然后返回到
DataList


我遗漏了什么吗?

@LarsTech感谢您的回复。为了使事情进一步复杂化,datagrid实际上位于tabcontrol中的一个选项卡上(每个选项卡上都有许多数据网格,每个都运行一个数据生成器。tabcontrol确实被添加到表单控件集合中,我将datagrid添加到选项卡的控件中。但是它似乎仍然不起作用。因此,我稍微简化了一些事情。该程序需要可变数量的tabpages/datagrids/datagenerator,因此它们是动态创建的。如果需要,我可以用它更新代码information@LarsTech我更新了表单代码,以更准确地显示正在发生的事情,包括动态生成选项卡页面和数据生成器。希望这有助于解决问题!我很想提供帮助,但您发布的代码中充满了错误<代码>me.Controls.Add(newTab)?newTab是一个选项卡页。StartGeneration会引发溢出异常。这些方法中缺少关键字
Sub
。属性1和2是字符串,您在传递数字。您对myDataGrids做什么?除此之外-您的代码工作正常。@LarsTech对错误表示抱歉,我想我在rus中键入的内容有点马虎h发布并获得解决方案。我在中添加了子关键字。我删除了me.controls.add(newTab)(这实际上不在我的代码中,我在再次匆忙时意外地将其放在了帖子中)。然后,示例中的数据生成并不重要。我只是将一些我认为会生成大量数据的内容组合在一起。我将其更改为使用线程生成数据。
Public class Form1
    Private myGenerators as new BindingList(of dataGenerator)
    Private myDataGrids as new BindingList(of DataGridView)
    Private Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Mybase.Load
          dim NumberOfGenerators as integer = Convert.ToInt32(My.Settings.CraneCount)
          for i as integer = 1 to NumberOfGenerators
              Dim newGenerator As New DataGenerator()
              Dim newTab as Ne tabPage(i.ToString)
              Dim NewGrid as New DataGridView
              newTab.Controls.Add(newGrid)
              newGrid.DataSource = newGenerator.DataList
              myGenerators.Add(newGrid)
          next
    End Sub
    Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStart.Click
        for each generator as dataGenerator in myGenerators
             generator.StartDataThread()
         next
    End Sub
    Private Sub ButtonChangeFormat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonChangeFormat.Click
        for each generator as dataGenerator in myGenerators
             generator.ChangeDataFormat()
         next
   End Sub
End Class