When NCRE was optimized, an error like this was reported: The line change already belongs to another table. The starting reason for the error is that I want to assign a row of a datatable table to another datatable.
The solution is: first add the corresponding column name to the datatable that receives the data
#region 为table1和table2加入对应的列名
for (int j = 0; j < dt.Columns.Count; j++)
{
table1.Columns.Add(dt.Columns[j].ColumnName);//有重载的方法,可以加入列数据的类型
}
for (int j = 0; j < dt.Columns.Count; j++)
{
table2.Columns.Add(dt.Columns[j].ColumnName);//有重载的方法,可以加入列数据的类型
}
#endregion
Then declare a row, assign a value to the row, and then add the row to the table.
for (int i = 1; i < dt.Rows.Count; i++)
{
studentInfo.studentID = Convert.ToString(dt.Rows[i][0]);
DataTable table = addStudent.SelectStudent(studentInfo);
if (table.Rows.Count > 0)
{
//声明一个行
DataRow drq = table1.NewRow();
drq.ItemArray = dt.Rows[i].ItemArray;
table1.Rows.Add(drq);
}
else
{
DataRow drq = table2.NewRow();
drq.ItemArray = dt.Rows[i].ItemArray;
table2.Rows.Add(drq);
}
}
In this way, a row in one datatable can be successfully assigned to another datatable.