使用非集合对象作为数据源数据源、对象

2023-09-04 01:00:04 作者:独归

这是一堆的dotnet框架组件的使用数据源组件。 我有一个对象,它有许多设置,可以修改它重新presents的数据源。我想这个对象设置为一组组合框和DataGridViewComboBoxCells的下拉列表中的数据源。

想真正挂钩的东西到组合框,当我的问题就来了。我想这是因为更改数据源可能发生,一旦数据源已定,我必须使用这些BindingSource的事情之一,但MSDN文学是拉告诉我,其一贯的恶作剧什么的BindingSource是瞒着我做什么或者它是如何工作的。

什么是你们可以建议挂钩这个对象作为一个数据源的最佳方法/ BindingSource的?

编辑: 显然,这个类是垃圾,但它说明了对象,我现在的排序。 大部分的时间是在空中的那一刻,但基本上什么这说明的是,我的课不是集合本身,而是包含一个。我需要能够指示组合框的DataSource属性是有挥发性的列表,在这里找到,而且它应该使用该列表作为数据源为它的下拉列表。

 公共类DynamicDataSource
    私人basicList作为新的列表(串)(新的String(){一,二,三,四有,五,六个一,七,八,九 , 十})
    私人_showEvensOnly为布尔
    私人_showNotContainingO为布尔
    公共财产ShowEvensOnly()作为布尔
        得到
            返回_showEvensOnly
        最终获取
        设置(BYVAL值作为布尔)
            _showEvensOnly =价值
        结束设定
    高端物业
    公共财产ShowNotContainingO()作为布尔
        得到
            返回_showNotContainingO
        最终获取
        设置(BYVAL值作为布尔)
            _showNotContainingO =价值
        结束设定
    高端物业
    公共功能GetDynamicList()方式列表(串)
        昏暗processMe作为新的列表(串)(basicList)
        如果Me._showEvensOnly然后
            对于JJ作为整数= processMe.Count  -  1 0步骤-1
                如果JJmod2 = 0然后
                    processMe.Remove(processMe(JJ))
                结束如果
            下一个
        结束如果

        如果Me._showNotContainingO然后
            对于JJ作为整数= processMe.Count  -  1 0步骤-1
                如果processMe(JJ).ToUpper.Contains(OC),然后
                    processMe.Remove(processMe(JJ))
                结束如果
            下一个
        结束如果

        返回processMe
    端功能
末级
 

解决方案

短版:使用的BindingList< T> ...

长版:

A 数据源是通常是:

在一个单独的对象(简单绑定) 列表源( IListSource ) 列表(的IList

由于您使用它为一个下拉,这听起来像你想的第二个二,一般的IList IListSource 是比较少见的,除了数据表)。

求解决 在sql 2008 中用GridView控件配置数据源时出现此类弹框提示为 未将对象引用设置到对象的实例

有关变化,一旦你已绑定,则需要通知。对于简单的绑定(单个对象),无论是 INotifyPropertyChanged的 *更改事件是要走的路 - 但列出了需要实施 IBindingList的,提高了的ListChanged 事件告诉发生了什么事的控制。

说实话,这是很多非有趣的工作,这是很容易使一个烂摊子。

务实的做法是用的BindingList&LT工作; 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?