Reputation: 793
I have a question. I have 2 DataTables and want to copy 3 columns from first table to 3 columns another table. How can i do that with skip 3 first rows and then add values to 2.table?.
My code looks like that:
foreach (DataRow ex1 in excelTb1.Rows)
{
foreach (DataRow ex2 in excelTb2.Rows)
{
//ex2["ABC"] = ex1["ABC"]; // with skip(3) ?
//ex2["Name"] = ex1["Name"];
//ex2["ID"] = ex1["ID"];
}
}
My 1.table:___________________________My 2.table should look likt this:
Table1 Table2
ABC Name ID ... ABC Name ID ...
a lola 2 ... ... ... ... ...
b kiki 6 ... ... ... ... ...
... ... ... ... ... ... ... ...
a lola 2 ...
b kiki 6 ...
... ... ... ...
Upvotes: 0
Views: 1300
Reputation: 3063
for (int i = 0; i < excelTb1.Rows.Count; i++)
{
DataRow dr1 = excelTb1.Rows[i];
if (excelTb2.Rows.Count > i + 3)
{
DataRow dr2 = excelTb2.Rows[i + 3];
dr2["ABC"] = dr1["ABC"];
dr2["Name"] = dr1["Name"];
dr2["ID"] = dr1["ID"];
}
else
break;
}
Upvotes: 1
Reputation: 223277
skip first 3 rows
Use a For loop instead of foreach.
for(int i=3; i < excelTb2.Rows.Count; i++) //start the loop with index 3 => Row 4
{
DataRow ex2 = exceltb2.Rows[i];
//ex2["ABC"] = ex1["ABC"]; // with skip(3) ?
//ex2["Name"] = ex1["Name"];
//ex2["ID"] = ex1["ID"];
}
Upvotes: 2