使用行和列从一个DataTable复制到另一个DataTable

时间:2014-03-10 10:57:39

标签: c#-4.0 datatable copy comparison

引用:从一个表复制到另一个表

我有2个数据表table1和table2 我想匹配参数名称,并将table2到table1的第5和第2个值复制到新列(p90和p10)中,以获取相同的参数名称如何解决问题?

Input
table1
  |Parameter Name| Lower limit     |Upper limit  |P90 point|P10 point|
     Diff_IC[1]  |  1.5            |    2.5      |         |          |
     Prog_IC[2]  |-10              |  10000000   |         |          |
     Nam_IC[3]   |-64              |64           |         |          |      
     ADCI_N[1 ]  |-0.8             | -0.1        |         |          |


table2
    Diff_IC[1]  |   Prog_IC[2]     |       Nam_IC[3]      | ADCI_N[1 ] |
  -0.145712003      -0.146583006         -0.165715003      -0.126583006
 -0.137499005      -0.137592003          -0.157493005      -0.117592003
 -0.142690003      -0.143250003          -0.132693003      -0.153250003
 -0.139434993      -0.140459001          -0.129434933      -0.150459001
 -0.147183999      -0.148519993          -0.117183459      -0.138519993
 -0.137183999      -0.134519993          -0.517183459      -0.338519993



 Output:
 table1
|Parameter Name| Lower limit| Upper limit      |P90 point       |P10 point           |
  Diff_IC[1]   |  1.5       |    2.5           |-0.147183999    |-0.137499005        |
  Prog_IC[2]   |-10         |  10000000        | -0.148519993   |  -0.137592003      |
  Nam_IC[3]    |-64         |64                | -0.117183459   |  -0.157493005      |
  ADCI_N[1 ]   |-0.8        | -0.1             |  -0.138519993  |   -0.117592003     |

1 个答案:

答案 0 :(得分:0)

检查此代码。这应该可以解决问题。我没有测试过,所以请这样做。

for (int i = 0; i < table1.Rows.Count; i++)
{
    string colVal = table1.Rows[i]["Parameter Name"].ToString();
    if (table2.Columns.Contains(colVal))
    {
        table1.Rows[i]["P90 point"] = table2.Rows[4][colVal].ToString();
        table1.Rows[i]["P10 point"] = table2.Rows[1][colVal].ToString();
    }
}

希望这有帮助。