【发布时间】:2014-01-12 07:33:51
【问题描述】:
我想使用 ImportRow 方法将旧数据表 (oldDT) 中的所有行复制到新数据表 (dt)。但是新行与旧行没有相同的列。这是我的代码:
foreach (DataRow dr in oldDT.Rows)
{
MessageBox.Show(dr["tenant_no"].ToString()); //giving a correct result
dt.ImportRow(dr);
MessageBox.Show(dt.Rows[0]["tenant_no"].ToString()); //giving an error Column 'tenant_no' does not belong to table .
}
我尝试使用答案C# simple way to copy or clone a DataRow?,这是我的新代码:
foreach (DataRow dr in oldDT.Rows)
{
MessageBox.Show(dr["tenant_no"].ToString());
DataRow newDR = oldDT.NewRow();
newDR.ItemArray = dr.ItemArray.Clone() as object[];
dt.Rows.Add(newDR); //giving an error "This row already belongs to another table."
MessageBox.Show(dt.Rows[0]["tenant_no"].ToString());
}
谁能帮帮我?
【问题讨论】: