使用非集合对象作为数据源
一堆 dotnet 框架组件使用 DataSource 组件。 我有一个对象,它有许多设置可以修改它所代表的数据源。 我想将此对象设置为一组 ComboBox 和 DataGridViewComboBoxCell 的下拉数据源。
当我尝试将东西实际连接到组合框中时,我的问题就出现了。 我猜想,因为一旦设置了 DataSource,就可能会发生对 DataSource 的更改,所以我必须使用这些 BindingSource 事物之一,但是 MSDN 文献正在拉起它通常的恶作剧,告诉我 BindingSource 是什么,但没有告诉我它的作用或者它是如何运作的。
你们建议将此对象连接为数据源/绑定源的最佳方法是什么?
编辑:
显然这个类是垃圾,但它说明了我现在拥有的对象类型。
目前大部分时间都悬而未决,但基本上这表明我的类本身不是一个集合,而是包含一个集合。 我需要能够指示 ComboBox 的 DataSource 属性在此处找到一个易失性列表,并且应该使用该列表作为其下拉列表的数据源。
Public Class DynamicDataSource
Private basicList As New List(Of String)(New String() {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"})
Private _showEvensOnly As Boolean
Private _showNotContainingO As Boolean
Public Property ShowEvensOnly() As Boolean
Get
Return _showEvensOnly
End Get
Set(ByVal value As Boolean)
_showEvensOnly = value
End Set
End Property
Public Property ShowNotContainingO() As Boolean
Get
Return _showNotContainingO
End Get
Set(ByVal value As Boolean)
_showNotContainingO = value
End Set
End Property
Public Function GetDynamicList() As List(Of String)
Dim processMe As New List(Of String)(basicList)
If Me._showEvensOnly Then
For JJ As Integer = processMe.Count - 1 To 0 Step -1
If JJ Mod 2 = 0 Then
processMe.Remove(processMe(JJ))
End If
Next
End If
If Me._showNotContainingO Then
For JJ As Integer = processMe.Count - 1 To 0 Step -1
If processMe(JJ).ToUpper.Contains("O"c) Then
processMe.Remove(processMe(JJ))
End If
Next
End If
Return processMe
End Function
End Class
A bunch of dotnet framework components use a DataSource component.
I have an object that has a number of settings that can modify the DataSource which it represents. I would like to set this object as the dropdown DataSource of a set of ComboBoxes and DataGridViewComboBoxCells.
My problem comes when trying to actually hook the thing into the ComboBox. I guess that because the changes to the DataSource can happen once the DataSource has been set, I have to use one of these BindingSource things, but the MSDN literature is pulling its usual prank of telling me what a bindingSource is without telling me what it does or how it works.
What's the best way you guys can suggest of hooking this Object up as a DataSource/BindingSource?
EDIT:
Obviously this class is junk, but it illustrates the sort of object I have now.
Most of the timing is up in the air at the moment, but basically what this shows is that my class is not a collection itself, but contains one. I need to be able to instruct the DataSource property of a ComboBox that there is a volatile list to be found here, and that it should use that list as the DataSource for its dropdown.
Public Class DynamicDataSource
Private basicList As New List(Of String)(New String() {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"})
Private _showEvensOnly As Boolean
Private _showNotContainingO As Boolean
Public Property ShowEvensOnly() As Boolean
Get
Return _showEvensOnly
End Get
Set(ByVal value As Boolean)
_showEvensOnly = value
End Set
End Property
Public Property ShowNotContainingO() As Boolean
Get
Return _showNotContainingO
End Get
Set(ByVal value As Boolean)
_showNotContainingO = value
End Set
End Property
Public Function GetDynamicList() As List(Of String)
Dim processMe As New List(Of String)(basicList)
If Me._showEvensOnly Then
For JJ As Integer = processMe.Count - 1 To 0 Step -1
If JJ Mod 2 = 0 Then
processMe.Remove(processMe(JJ))
End If
Next
End If
If Me._showNotContainingO Then
For JJ As Integer = processMe.Count - 1 To 0 Step -1
If processMe(JJ).ToUpper.Contains("O"c) Then
processMe.Remove(processMe(JJ))
End If
Next
End If
Return processMe
End Function
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短版本:使用
BindingList
...长版本:
DataSource
通常是:IListSource
)IList
)由于您将其用于下拉列表,听起来您想要后两个之一,通常是
IList
(< code>IListSource 相对较少,除了DataTable
)。对于绑定后的更改,您需要通知。 对于简单的绑定(单个对象),
INotifyPropertyChanged
或*Changed
事件都是可行的方法 - 但对于列表,您需要实现IBindingList
和引发ListChanged
事件来告诉控件发生了什么。说实话,这是很多无趣的工作,很容易搞砸。
务实的方法是使用
BindingList
(可能继承自它)。 这将为您提供所有列表通知,包括支持列表中的项目发生更改(如果您在项目上实施INotifyPropertyChanged
)(它不支持*Changed事件)。
警告:并非所有控件都关心通知......如果他们不关心通知,那么您就无能为力。 因此,如果即使使用
BindingList
也看不到添加/交换/等 - 或者在实现INotifyPropertyChanged
时看不到项目更新,那么...呃,强硬吗?Short version: use
BindingList<T>
...Long version:
A
DataSource
is typically either:IListSource
)IList
)Since you are using it for a drop-down, it sounds like you want one of the second two, typically
IList
(IListSource
is relatively rare, except forDataTable
).For changes once you have bound, you need notifications. For simple bindings (individual objects), either
INotifyPropertyChanged
or*Changed
events are the way to go - but for lists you need to implementIBindingList
and raise theListChanged
event to tell the control what happened.To be honest, this is a lot of non-interesting work that it is very easy to make a mess of.
The pragmatic approach is to work with
BindingList<T>
(possibly inheriting from it). This gives you all the list notifications, including support for items in the list changing if you implementINotifyPropertyChanged
on the items (it doesn't support*Changed
events, though).Caveat: not all controls care about notifications... and if they don't there isn't a lot you can do about it. So if you don't see additions/swaps/etc even when using
BindingList<T>
- or you don't see item updates when implementINotifyPropertyChanged
, then... er, tough?