When we trying to add a DataTable (that is inside of a different DataSet) to new DataSet ds. If I add it directly, we will get the following error:
DataTable already belongs to another DataSet.
DataTable.Copy
use DataTable.Copy to create a copy of your data table; then insert the copy into the target DataSet:
dataSetX.Tables.Add( dataTableFromDataSetY.Table[0].Copy() );
DataSet.Merge
You could also use DataSet.Merge for this:
dataSetX.Merge(dataTableFromDataSetY);
Note, however, that if you are going to use this method, you might want to make sure that your target DataSet doesn't already contain a table with the same name:
If the target DataSet doesn't contain a table by the same name, a fresh copy of the table is created inside the data set;
If a table by the same name is already in the target data set, then it will get merged with the one passed to Merge, and you end up with a mix of the two.