【问题标题】:Bind combobox to Parent DataRelation and update when 2nd Parent changes将组合框绑定到父 DataRelation 并在第二个父更改时更新
【发布时间】:2016-09-01 10:19:21
【问题描述】:

我环顾四周,找不到我需要的东西。

我有一个带有(以及其他)3 个表的数据库。
SchemeType
Scheme
Type

SchemeType 包含SchemeType 的主键的外键

我有一个包含 2 个组合框的 .NET 3.5 WinForm。
一个显示Schemes
我希望另一个显示存在于SchemeType 表中的不同Types 用于选定的Scheme

我有一个DataSet,其中包含所有 3 个表的所有条目,并为主外键关系设置了 DataRelations。

我正在使用 BindingSources 来填充组合框,但是当我更改 Scheme 组合框时,我无法弄清楚如何让 Type 组合框刷新其内容。

我可以使用直接的父子关系来做到这一点,但不知道如何编码父子关系。
这是我的代码,去掉了不必要的东西

    Dim DS As New DataSet("myDS")
    Dim SchemeBndSrc As New BindingSource
    Dim TypeBndSrc As New BindingSource

    Using cmd As New SqlCommand("myStroedProc", _conn)
        cmd.CommandType = CommandType.StoredProcedure
        Using adp As New SqlDataAdapter(cmd)
            adp.Fill(DS)
        End Using
    End Using

    ' Name the tables
    DS.Tables(0).TableName = "Scheme"
    DS.Tables(1).TableName = "Type"
    DS.Tables(2).TableName = "SchemeType"

    Dim rel As New DataRelation("Scheme-SchemeType", _
                                 DS.Tables("Scheme").Columns("SchemeID"), _
                                 DS.Tables("SchemeType").Columns("SchemeID"), _
                                 True)

    Dim rel2 As New DataRelation("Type-SchemeType", _
                                DS.Tables("Type").Columns("TypeID"), _
                                DS.Tables("SchemeType").Columns("TypeID"), _
                                True)

    DS.Relations.Add(rel)
    DS.Relations.Add(rel2)



    ' Scheme
    ' Set up the binding source
    SchemeBndSrc.DataSource = DS
    SchemeBndSrc.DataMember = "Scheme"

    ' Bind the bindingsource to the combobox
    cboScheme.ValueMember = "SchemeId"
    cboScheme.DisplayMember = "SchemeName"
    cboScheme.DataSource = SchemeBndSrc
    cboScheme.SelectedIndex = -1

    ' Type
    ' Set up the binding source
    TypeBndSrc.DataSource = SchemeBndSrc
    TypeBndSrc.DataMember = "Type-SchemeType"

    ' Bind the bindingsource to the combobox
    cboType.ValueMember = "TypeID"
    cboType.DisplayMember = "TypeDesc"
    cboType.DataSource = TypeBndSrc
    cboType.SelectedIndex = -1

类型组合框不包含任何项目,即使其中应该至少有 1 个项目。如果我交换 DataRelation,它不会将其添加到 DataSet,因为在这种情况下,父级 (SchemeType) 没有 TypeID 的唯一条目。

有人可以帮帮我吗?

【问题讨论】:

    标签: c# vb.net winforms combobox datarelation


    【解决方案1】:

    您不会通过数据绑定自动完成此操作。数据绑定可以处理基于所选父级过滤子列表,因此您可以获得SchemeType 列表以根据所选Scheme 自动过滤。然后,您想要的是根据这些子记录获取所有父 Type 记录,而数据绑定不会这样做。那必须是手动的。

    像往常一样将SchemeSchemeType 表绑定到BindingSources 作为父子和子,子BindingSource 通过父BindingSource 绑定到DataRelation。一旦选择了Scheme 并且子BindingSource 会自动过滤,您可以循环遍历它以获取Type 记录的所有ID,并使用它为第三个BindingSource 构建Filter 值,例如

    Dim typeIDs = schemeTypeBindingSource.Cast(Of DataRowView)().
                                          Select(Function(drv) CInt(drv("TypeID")))
    
    typeBindingSource.Filter = String.Fomrat("TypeID IN ({0})",
                                             String.Join(", ", typeIDs))
    

    【讨论】:

    • 啊,谢谢。我担心它必须是手动的,但想检查一下我是否错过了一种自动方式。我不能同时绑定每个组合的选定值以便能够使用 SQLDataAdapter.Update 进程,这对吗?
    • 我不能 100% 确定如果不进行测试会产生什么影响。我不确定您是否可以使用绑定来过滤和更新。
    【解决方案2】:

    我知道这是几年后的事了,但我会这样做:

    在 SchemeType 数据表上,添加一个名为 ParentTypeDesc 的列,并将其 .Expression 属性设置为 Parent(Type-SchemeType).TypeDesc - 这意味着 SchemeType 表将为每个 TypeID 将父名称导入到自身中

    然后设置:

    SchemeBindSrc.DataSource = theDataSet
    SchemeBindSrc.DataMember = "Scheme"
    

    还有魔法:

    TypeBindSrc.DataSource = SchemeBindSrc 
    TypeBindSrc.DataMember = "Scheme-SchemeType" 'the name of the datarelation
    

    那么绑定到 TypeBindSrc 的组合具有:

    TypeCombo.DisplayMember = "ParentTypeDesc" ' the name of the column that imports the type name along the Type-SchemeType relation 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2013-04-17
      • 2021-06-30
      • 2021-10-03
      • 2011-09-19
      • 2017-06-03
      相关资源
      最近更新 更多