DataTable 已经属于另一个 DataSet [英] DataTable already belongs to another DataSet
问题描述
将一个数据表从一个数据集中添加到另一个数据表时发生此错误.DataTable 已经属于另一个 DataSet."
This error is occuring while adding one datatable from a dataset to another ."DataTable already belongs to another DataSet."
dsformulaValues.Tables.Add(m_DataAccess.GetFormulaValues
(dv.ToTable.DefaultView.ToTable(False, strSelectedCols)).Tables(0))
推荐答案
就像其他回复指出的那样,您看到的错误是因为您尝试添加到 DataSet 的 DataTable 已经属于不同的数据集.
Like the other responses point out, the error you're seeing is because the DataTable you're attempting to add to a DataSet is already a part of a different DataSet.
一种解决方案是复制 DataTable 并将副本分配给另一个DataSet.
One solution is to Copy the DataTable and assign the copy to the other DataSet.
dtCopy = dataTable.Copy()
ds.Tables.Add(dtCopy)
复制的DataTable将具有复制的DataTable的结构和数据.
The copied DataTable will have the structure and data of the copied DataTable.
如果您只想要 DataTable 的结构,请调用 克隆.
If you only want the structure of the DataTable, call Clone instead.
dtCopy = dataTable.Clone()
这篇关于DataTable 已经属于另一个 DataSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!