首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BindingSource和父DataRelation

BindingSource和父DataRelation
EN

Stack Overflow用户
提问于 2013-03-06 09:58:33
回答 1查看 1.9K关注 0票数 1

我有4个DataTables:

  • 产品: pk_product,name,fk_color,fk_size。
  • 颜色: pk_color,名字。
  • 大小: pk_size,姓名。
  • 日志: pk_log,fk_product,date。

产品表绑定到“主”BindingSource。将日志表(子关系)绑定到主绑定就像一种魅力。但是绑定颜色和大小表(父关系)失败。

我做错什么了?

代码语言:javascript
复制
    Dim data As New DataSet()

    With data.Tables.Add("COLOR")
        .Columns.AddRange(New DataColumn() {New DataColumn("PK_COLOR", GetType(Int32)), New DataColumn("NAME", GetType(String))})
        .PrimaryKey = New DataColumn() {.Columns(0)}
        .BeginLoadData()
        For pk_color As Integer = 1 To 5
            .Rows.Add(pk_color, String.Format("Color #{0}", pk_color))
        Next
        .EndLoadData()
        .AcceptChanges()
    End With

    With data.Tables.Add("SIZE")
        .Columns.AddRange(New DataColumn() {New DataColumn("PK_SIZE", GetType(Int32)), New DataColumn("NAME", GetType(String))})
        .PrimaryKey = New DataColumn() {.Columns(0)}
        .BeginLoadData()
        For pk_size As Integer = 6 To 10
            .Rows.Add(pk_size, String.Format("Size #{0}", pk_size))
        Next
        .EndLoadData()
        .AcceptChanges()
    End With

    With data.Tables.Add("PRODUCT")
        .Columns.AddRange(New DataColumn() {New DataColumn("PK_PRODUCT", GetType(Int32)), New DataColumn("NAME", GetType(String)), New DataColumn("FK_COLOR", GetType(Int32)), New DataColumn("FK_SIZE", GetType(Int32))})
        .PrimaryKey = New DataColumn() {.Columns(0)}
        .BeginLoadData()
        Dim pk_product As Integer = 1
        For fk_color As Integer = 1 To 5
            For fk_size As Integer = 6 To 10
                .Rows.Add(pk_product, String.Format("Product #{0}", pk_product), fk_color, fk_size)
                pk_product += 1
            Next
        Next
        .EndLoadData()
        .AcceptChanges()
    End With

    With data.Tables.Add("LOG")
        .Columns.AddRange(New DataColumn() {New DataColumn("PK_LOG", GetType(Int32)), New DataColumn("FK_PRODUCT", GetType(Int32)), New DataColumn("DATE", GetType(DateTime))})
        .PrimaryKey = New DataColumn() {.Columns(0)}
        .BeginLoadData()
        For pk_log As Integer = 1 To 15
            .Rows.Add(pk_log, pk_log, DateTime.Now().AddDays(CDbl(pk_log * -1)))
        Next
        .EndLoadData()
        .AcceptChanges()
    End With

    Dim productColorRelation As DataRelation = data.Relations.Add("PRODUCT_FK_COLOR", data.Tables("COLOR").Columns("PK_COLOR"), data.Tables("PRODUCT").Columns("FK_COLOR"))
    Dim productSizeRelation As DataRelation = data.Relations.Add("PRODUCT_FK_SIZE", data.Tables("SIZE").Columns("PK_SIZE"), data.Tables("PRODUCT").Columns("FK_SIZE"))
    Dim logProductRelation As DataRelation = data.Relations.Add("LOG_FK_PRODUCT", data.Tables("PRODUCT").Columns("PK_PRODUCT"), data.Tables("LOG").Columns("FK_PRODUCT"))

    data.AcceptChanges()

    'Master binding:
    Dim productBinding As BindingSource = New BindingSource(data, "PRODUCT")

    'Parent bindings: (Do NOT work)
    Dim productColorBinding As BindingSource = New BindingSource(productBinding, productColorRelation.RelationName)
    Dim productSizeBinding As BindingSource = New BindingSource(productBinding, productSizeRelation.RelationName)

    'Child binding:
    Dim logProductBinding As BindingSource = New BindingSource(productBinding, logProductRelation.RelationName)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-26 16:49:40

如果只需要与颜色和大小的关系来显示相关数据,则可以尝试反转这些关系并禁用check约束。定义这样的关系:

代码语言:javascript
复制
Dim productColorRelation As DataRelation = data.Relations.Add("PRODUCT_FK_COLOR",  data.Tables("PRODUCT").Columns("FK_COLOR"),data.Tables("COLOR").Columns("PK_COLOR"),false)
Dim productSizeRelation As DataRelation = data.Relations.Add("PRODUCT_FK_SIZE", data.Tables("PRODUCT").Columns("FK_SIZE"), data.Tables("SIZE").Columns("PK_SIZE"), false)

'Parent bindings: (This should work with the reverse relations)
Dim productColorBinding As BindingSource = New BindingSource(productBinding, productColorRelation.RelationName)
Dim productSizeBinding As BindingSource = New BindingSource(productBinding, productSizeRelation.RelationName)

祝好运!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15244085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档