Skip to main content
 首页 » 编程设计

c#之将数据行从一个数据表插入到特定索引

2024年02月13日11mate10pro

<分区>

我正在从我的数据库中检索 DataTables,我需要将一个 DataTable 中的特定行插入到另一个 DataTable 的确定索引中.

基本上我想说的是,DataTable dt1 的行 [0] 插入到索引 4 处的 DataTable dt2。

这是从两个不同的数据库调用中获得的两个不同的表。有办法做到这一点吗?

如有任何帮助,我们将不胜感激。

[编辑] 抱歉,这是我所做的

DataTable dt1 = populateTable(); 
DataTable targetTable = populateTargetTable(); 
DataRow dr; 
dr = dt1.NewRow(); 
dr = targetTable.Rows[0]; 
 
int index=0; 
DataRow temp; 
for (int i = 0; i < dt1.Rows.Count; i++) 
{ 
    temp = dt1.Rows[i]; 
    if (rowFitsCondition(temp)){ 
        index = i; 
    } 
} 
dt1.Rows.InsertAt(dr, index); 

并且 InsertAt() 调用失败,并给我 This row already belongs to another table 错误。