0

我有一堂课bindinglist(of T)bindinglist绑定到我的datagridview表单上。将项目添加到 时bindinglist,它们会显示在 中,datagridview但是scrollbar永远不会更改以适应新数据。我开始认为这是因为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

形式:

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

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

我错过了什么吗?

4

0 回答 0