首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将数据绑定到DataRelation?

如何将数据绑定到DataRelation?
EN

Stack Overflow用户
提问于 2018-08-05 05:18:20
回答 1查看 325关注 0票数 0

我有以下代码来创建两个绑定源。第一个是用主表填充的,第二个是基于关系的子表。

代码语言:javascript
复制
    Dim DataSet As New DataSet
    Dim BindingSource1 As New BindingSource
    Dim BindingSource2 As New BindingSource

    Dim DataTable1 As DataTable = GetDT1()
    Dim DataTable2 As DataTable = GetDT2()

    DataSet.Tables.Add(DataTable1)
    DataSet.Tables.Add(DataTable2)

    Dim Rel As DataRelation = New DataRelation("R", DataSet.Tables("DT1").Columns("C1"), DataSet.Tables("DT2").Columns("C1"), True)
    DataSet.Relations.Add(Rel)

    BindingSource1.DataSource = DataSet
    BindingSource1.DataMember = "DT1"

    BindingSource2.DataSource = DataSet
    BindingSource2.DataMember = "R"

其中GetDT1和GetDT2如下所示:

代码语言:javascript
复制
Public Function GetDT1() As DataTable
    Dim DT As New DataTable("DT1")

    DT.Columns.Add(New DataColumn("C1"))

    Dim R As DataRow = DT.NewRow

    R("C1") = 1
    DT.Rows.Add(R)

    R = DT.NewRow
    R("C1") = 2
    DT.Rows.Add(R)

    R = DT.NewRow
    R("C1") = 3
    DT.Rows.Add(R)

    Return DT
End Function

Public Function GetDT2() As DataTable
    Dim DT As New DataTable("DT2")

    DT.Columns.Add(New DataColumn("C1"))
    DT.Columns.Add(New DataColumn("C2"))

    Dim R As DataRow = DT.NewRow

    R("C1") = 1
    R("C2") = "A"
    DT.Rows.Add(R)

    R = DT.NewRow
    R("C1") = 1
    R("C2") = "B"
    DT.Rows.Add(R)

    R = DT.NewRow
    R("C1") = 2
    R("C2") = "Coucou"
    DT.Rows.Add(R)

    Return DT
End Function

当我运行代码时,我的代码行中有一个错误:BindingSource2.DataMember = "R"。上面写着:

在DataSource上找不到DataMember属性“R”。

有人知道我的代码出了什么问题吗?

干杯,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-05 09:09:11

这就是答案:

代码语言:javascript
复制
Dim DataSet As New DataSet
Dim BindingSource1 As New BindingSource
Dim BindingSource2 As New BindingSource

Dim DataTable1 As DataTable = GetDT1()
Dim DataTable2 As DataTable = GetDT2()

DataSet.Tables.Add(DataTable1)
DataSet.Tables.Add(DataTable2)

Dim Rel As DataRelation = New DataRelation("R", DataSet.Tables("DT1").Columns("C1"), DataSet.Tables("DT2").Columns("C1"), True)
DataSet.Relations.Add(Rel)

BindingSource1.DataSource = DataSet
BindingSource1.DataMember = "DT1"

BindingSource2.DataSource = BindingSource1
BindingSource2.DataMember = "R"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51689631

复制
相关文章

相似问题

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