1

私は4つのDataTablesを持っています:

  • 製品: pk_product、名前、fk_color、fk_size。
  • COLOR: pk_color、名前。
  • SIZE: pk_size、名前。
  • ログ: pk_log、fk_product、日付。

製品テーブルは「マスター」BindingSource にバインドされます。ログテーブル (子リレーション) をマスター バインディングにバインドすることは魅力のように機能します。ただし、色とサイズのテーブル (親関係) のバインドは失敗します。

私は何を間違っていますか?

    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)
4

1 に答える 1

3

関連データを表示するためだけに色とサイズの関係が必要な場合は、これらの関係を逆にしてチェック制約を無効にすることができます。次のようにリレーションを定義します。

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)

幸運を!

于 2014-02-26T16:49:40.900 に答える