Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将DataRow[]转换为DataTable而不丢失其数据集_C#_Datatable_Dataset_Datarow - Fatal编程技术网

C# 将DataRow[]转换为DataTable而不丢失其数据集

C# 将DataRow[]转换为DataTable而不丢失其数据集,c#,datatable,dataset,datarow,C#,Datatable,Dataset,Datarow,我正在使用C#中的数据集和数据表 我的第一个问题是如何将DataRow[]对象转换为DataTable,我通过以下链接解决了这个问题: 因此,我所做的是: // Gets the rows according to the relation DataRow[] rows = someDataRow.GetChildRows("the_table_relation"); DataTable newDataTable = rows.CopyToDataTable<DataRow>();

我正在使用C#中的数据集和数据表

我的第一个问题是如何将DataRow[]对象转换为DataTable,我通过以下链接解决了这个问题:

因此,我所做的是:

// Gets the rows according to the relation
DataRow[] rows = someDataRow.GetChildRows("the_table_relation");
DataTable newDataTable = rows.CopyToDataTable<DataRow>();    
//根据关系获取行
DataRow[]rows=someDataRow.GetChildRows(“表关系”);
DataTable newDataTable=行。CopyToDataTable();
我处理的所有数据都在一个数据集中,因此,我的问题是我的对象
newDataTable
不再包含对我的数据集的引用(即
newDataTable.DataSet
等于
null

有没有一种方法可以在不丢失对我的数据集的引用的情况下进行转换??

希望有人能帮助我

提前感谢

假设您已经有了一个
数据表
?如果是,请使用将现有表作为参数的:

rows.CopyToDataTable<DataRow>(existingTable, LoadOption.Upsert);

我相信Jon Skeet的回答中可能包含了解决方案
dataSet.Tables.Add(newDataTable)


问题是newDataTable没有DataSet子控件,而是DataSet父控件。也就是说,一个数据集可以包含表,而newDataTable的数据集是“拥有newDataTable的数据集”,对于您在上面创建的表,在添加到数据集之前,该数据集是空的。

请告诉我们有关此数据集的更多信息,您已经有了…没有什么特别之处,这只是一个数据集,其中包含一些表以及它们之间的关系谢谢您的回复,但不幸的是,出于某些原因,我需要将DataRow[]对象转换为一个新的DataTable,而不会丢失其数据集引用
 DataView DV = MyDataTable.AsDataView();
 DV.RowFilter = "Id = '" + RecordId + "'";
 DataTable newDataTable = DV.ToTable();
 DataView DV = MyDataTable.AsDataView();
 DV.RowFilter = "Id = '" + RecordId + "'";
 DataTable newDataTable = DV.ToTable();