使用Win Forms编辑具有dataRelation的dataTable

时间:2014-03-08 21:06:01

标签: vb.net visual-studio-2012

我无法解决数据集异常: “不能在不同数据集中的表之间建立关系”

form1创建数据集,从dataSource填充2个dataTable,将它们添加到dataSet,然后创建dataRelation。

然后我将1个dataTables设置为form2的属性,然后将showDialog()设置为打开form2。当我尝试在form2上编辑dataTable的行时,我得到了dataSet异常。

任何人都知道为什么会这样吗? 谢谢!

class form1

    private dts as object

Public Property currentDts() As object
    Get
        Return dts
    End Get
    Set(ByVal value As Object)
        dts = value
    End Set
End Property

Private Sub form1_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load    
    dim ds as dataset = new dataset()

    dim ta as new mainDataSetTableAdapters.myTableAdapter   
    dim tbl as datatable = ta.getData()
    tbl.tablename = "foo"
    ds.tables.add(tbl)

    dim ta2 as new mainDataSetTableAdapters.myTableAdapter2
    dim tbl2 as datatable = ta2.getData()
    tbl2.tablename = "bar"
    ds.tables.add(tbl2)

    ds.relations.add(New dataRelation("bazRelation", _
                          ds.tables("foo").columns("id"), _ 
                          ds.tables("bar").columns("id"),false))

    dim dv as dataview = new dataView(ds.tables("bar"))
    dv.rowFilter = "Parent(bazRelation).id > 0"

    datagridView1.dataSource = dv
    me.currentDts = ds  
End Sub

Private Sub button1_click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles button1.click

    dim ds as dataset = me.currentDts
    dim qString as string = "ID = 5 OR ID = 6"

    form2.objPassedHere = {ds.tables("bar"), qString}
    form2.showDialog()
end sub

end class






class form2
    private parentDt as dataTable
    private params as object
    private bs as bindingSource = new bindingSource()

Public Property objPassedHere() As object
    Get
        Return params
    End Get
    Set(ByVal value As Object)
        params = value
    End Set
End Property

Private Sub form1_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    dim theBarTable as datatable = params(0)
    bs.dataSource = theBarTable
    bs.filter = params(1)
    datagridview1.datasource = bs
end sub

Private Sub textBox1_Leave(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles textBox1.leave
    dim rowView as dataRowView = bs.current
    dim row as datarow = rowView.row

    row.item("personName") = textBox1.text  
    'ERROR: cannot have a relationship between tables in different datasets!!!!!!
end sub

end class

0 个答案:

没有答案