문제

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     ...        
                                         ...   ...    ...   ...             
도움이 되었습니까?

해결책

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"];
}

다른 팁

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top