[英]Copy one datatable into another
I have 1st datatable as: 我有第一个数据表:
dtOld dtOld
Columns: 列:
CE CE
second datatable as: 第二个数据表:
dtNew: dtNew:
ABCDEF ABCDEF
I just wanted to copy stOld data into dtNew with specified columns. 我只想将stOld数据复制到带有指定列的dtNew中。
ie: 即:
C col's value of dtOld should copy in D col value of dtNew..and E Col to F col C col的dtOld值应该在DtNew ..和E Col的D col值中复制到F col
I tried some complex logic, but it does not worked. 我尝试了一些复杂的逻辑,但它没有用。
Is there any way to solve this. 有没有办法解决这个问题。
I tried as: 我试过:
DataTable dtOld = new DataTable();
dtOld = ds.Tables[0];
string[] columns = { "c", "E" };
string[] columns2 = { "D", "F " };
foreach (DataRow sourcerow in dtOld.Rows)
{
DataRow destRow = dtNew.NewRow();
int cntm = 0;
foreach (string colname in columns)
{
destRow[columns2[cntm]] = sourcerow[colname];
cntm++;
}
dtNew.Rows.Add(destRow);
}
Please help me. 请帮我。
It is just a simple copy from a fixed set of columns to another set, so why to complicate things? 它只是从一组固定的列到另一组的简单副本,那么为什么要复杂化呢?
foreach (DataRow sourcerow in dtOld.Rows)
{
DataRow destRow = dtNew.NewRow();
destRow["D"] = sourcerow["c"];
destRow["F"] = sourcerow["E"];
dtNew.Rows.Add(destRow);
}
foreach (DataRow row in dtOld.Rows)
dtNew.LoadDataRow(new[]
{
null, // default value
null,
null,
row["C"],
null,
row["E"]
}, LoadOption.Upsert);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.