使用非收集对象作为DataSource [英] Using a non-collection object as a DataSource

查看:145
本文介绍了使用非收集对象作为DataSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一堆dotnet框架组件使用DataSource组件。
我有一个对象有多个设置可以修改它代表的DataSource。我想将此对象设置为一组ComboBoxes和DataGridViewComboBoxCells的下拉列表DataSource。



我的问题来自于尝试将事情挂钩到ComboBox中。我猜是因为数据源的改变可能会在DataSource被设置之后发生,我必须使用这些BindingSource之一的东西,但MSDN的文献正在拉扯一般的恶作剧,告诉我一个bindingSource是什么,而不告诉我它是什么或者它如何工作。



您可以建议将此对象挂起为DataSource / BindingSource的最佳方式是什么?



编辑:

显然,这个类是垃圾,但它说明了我现在的类型。

大部分的时间在空中,但是基本上这表明我的类本身不是一个集合,而是包含一个。我需要能够指示ComboBox的DataSource属性,这里有一个volatile列表,它应该使用该列表作为DataSource的下拉列表。

 公共类DynamicDataSource 
私有basicList作为新列表(String)(New String(){one 二,三,四,五六七八十九十十五)
私人_showEvensOnly作为布尔
私人_showNotContainingO As Boolean
公共属性ShowEvensOnly()As Boolean
获取
返回_showEvensOnly
结束Get
Set(ByVal value As Boolean)
_showEvensOnly = value
结束集
结束属性
公共属性ShowNotContainingO()As Boolean
获取
返回_showNotContainingO
结束Get
Set(ByVal value As Boolean)
_showNotContainingO = value
结束集
结束属性
公共函数GetDynamicList()作为List(Of String)
Dim processMe作为新列表(String)(basicList)
如果Me._showEvensOnly然后
对于JJ As Integer = processMe.Count - 1 To 0 Step -1
如果JJ Mod 2 = 0然后
processMe.Remove(processMe(JJ))
结束如果
下一个
End If

如果Me._showNotContainingO然后
对于JJ As Integer = processMe.Count - 1 To 0 Step -1
如果processMe(JJ).ToUpper.Contains( Oc)然后
processMe.Remove(processMe(JJ))
End If
Next
End If

返回processMe
结束函数
结束类


解决方案

简短版本:使用 BindingList< T> ...



长版本:



A DataSource 通常是:




  • 一个单独的对象简单绑定)

  • 列表源( IListSource

  • 列表( IList



由于您正在使用它作为下拉菜单,听起来您希望其中的第二个,通常 IList IListSource 相对较少,除了 DataTable )。



一旦绑定了更改,您需要通知。对于简单的绑定(单个对象), INotifyPropertyChanged *更改事件是可行的 - 但是对于列表需要实现 IBindingList 并提出 ListChanged 事件来告诉控件发生了什么。


说实话,这是一个非常有趣的工作,很容易让人混乱。



实用的方法是使用 BindingList< T> (可能从它继承)。这将为您提供所有列表通知,包括对中的项目的支持,如果您在项目上实现 INotifyPropertyChanged (不支持 *更改事件,但是)



注意事项:并不是所有的控件都关心通知...如果他们不你可以做很多事情,所以,即使使用 BindingList< T> - 或者在实现 INotifyPropertyChanged时您看不到项目更新,您看不到添加/交换/ ,然后...呃,艰难?​​


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

解决方案

Short version: use BindingList<T>...

Long version:

A DataSource is typically either:

  • an individual object (for simple binding)
  • a list source (IListSource)
  • a list (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 for DataTable).

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 implement IBindingList and raise the ListChanged 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 implement INotifyPropertyChanged 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 implement INotifyPropertyChanged, then... er, tough?

这篇关于使用非收集对象作为DataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆