简体   繁体   中英

copying values of a dataTable to another DataTable with different clumns

I have a DataTable dt1 that contains this columns : PRODUCT_ID,MIN_VALUE,MAX_VALUE,AMOUNT and another DataTable dt2 that contains this columns : ID,MIN,MAX,POINT_TO_ADD

dt1 contains multiple rows that I want to copy them to dt2 how can I do that ?

try this

foreach (DataRow sourcerow in dt1.Rows)
{
    DataRow destRow = dt2.NewRow();
    destRow["ID"] = sourcerow["PRODUCT_ID"];
    destRow["MIN"] = sourcerow["MIN_VALUE"];
    destRow["MAX"] = sourcerow["MAX_VALUE"];
    destRow["POINT_TO_ADD"] = sourcerow["AMOUNT"];
    dt2.Rows.Add(destRow);
}

Try this:

for(int i=0;i<dt1.Rows.Count;i++){
   DataRow dr = dt2.NewRow();
   dr["ID"] = dt1.Rows[i]["PRODUCT_ID"];
   dr["MIN"] = dt1.Rows[i]["MIN_VALUE"];
   dr["MAX"] = dt1.Rows[i]["MAX_VALUE"];
   dr["POINT_TO_ADD"] = dt1.Rows[i]["AMOUNT"];
   dt2.Rows.Add(dr);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM