绑定列表(类)到 vb.net 中的 datagridview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22235864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
binding list(of class) to datagridview in vb.net
提问by BinaryDuck
I have a class that contains a list of data that gets populated from a datastream. I want to bind that list to a datagridview so it automatically populates as the list does. However the list isn't refreshing as the data comes in. I have something like this.
我有一个包含从数据流填充的数据列表的类。我想将该列表绑定到 datagridview,以便它像列表一样自动填充。然而,随着数据的进入,列表并没有刷新。我有这样的事情。
Public class MyClass
Private mData as list(Of networkData)
Public Property Data() as list(Of networkData)
Get
return mData
End Get
Set
mData = value
End Set
End Property
' some other properties that aren't imporant
' stuff to load Data with data from network stream
end class
Public class networkDat
Private rawdata as string
Public Property rawdata() as string
Get
return mrawdata
End Get
Set
mrawData = value
End Set
End Property
' some other properties that aren't imporant
' functions to parse rawdata into the other properties
End Class
'form
Public Class dataviewer
Dim dataView as datagridViewer = new datagridviewer()
Private Sub dataviewer_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dim x as myClass = new myClass() 'new will start the datastream
datagridview.datasource = x.Data
End Sub
Since I started the datastream first dataviewer will have an inital set of data. However it doesn't update as new data comes in.
自从我启动数据流以来,第一个数据查看器将拥有一组初始数据。但是,它不会随着新数据的进入而更新。
采纳答案by LarsTech
The List class does not implement the IBindingList interface which supports list change notifications.
List 类没有实现支持列表更改通知的 IBindingList 接口。
Try using the BindingListclass instead:
尝试使用BindingList类:
Provides a generic collection that supports data binding.
提供支持数据绑定的泛型集合。
To observe changes to the properties in your class, the class would need to implement the INotifyPropertyChangedinterface:
要观察类中属性的更改,该类需要实现INotifyPropertyChanged接口:
Notifies clients that a property value has changed.
通知客户端属性值已更改。
回答by Steve
List(of ) does not support change notifications. For your purpose, change the List(of ) to a ObservableCollection(of ).
List(of ) 不支持更改通知。为了您的目的,将 List(of ) 更改为 ObservableCollection(of )。

