Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Copying from one table to another
I have 2 datatable table1 and table2
I want match the Parameter names and copy the 5th and 2nd value from table2 to table1 into a new column(p90 and p10) for same parameter Name how to approach the problem ?

Input
table1
|Parameter Name| Lower limit     |Upper limit  |
   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     |


[Edit] Added from comment :
C#
for (int i = 0; i < +table1.Rows.Count; i++) {
   string col = table1.Rows[i]["Par Name"].ToString() 
   if (table2.Columns.Contains(col)) {
      table1.Rows[i]["point90"] = table2.Rows[4]["col"].ToString(); //Error here col does not exits in current context
   }
}

[/Edit]
Posted
Updated 10-Mar-14 23:14pm
v6
Comments
FarhanShariff 10-Mar-14 11:02am    
for (int i = 0; i < table1.Rows.Count; i++)
{
string col = table1.Rows[i]["Parameter Name"].ToString();

if(table2.Columns.Contains(col))
{
table1.Rows[i]["point90"] =table2.Rows[4]["col"].ToString();//Error here

}

}
Maciej Los 10-Mar-14 18:04pm    
Based on what condition? Would you explain a bit more? Let's say we have Diff_IC[1] parameter. Why do you get -0.147183999 value in P90 point column. There are several values? Why this one?
FarhanShariff 11-Mar-14 4:23am    
It is just an example I have around 2000 values. It is basically Percentile-90 point & Percentile 10 point

C#
for (int i = 0; i < +table1.Rows.Count; i++) {

There is an extra + in front of table1; not a big deal, though.

C#
string col = table1.Rows[i]["Par Name"].ToString()

Two problems here:
- a ; is missing at the end of the line
- should not the name of the parameter be ["Parameter Name"] instead of ["Par Name"] (according to the rest of your post)?

And, most important, as the line where you are pointing the error is synctatically correct :
which error? Would you mind sharing it with us?
 
Share this answer
 
foreach (DataRow dr1 in table1.Rows)
{
string para = dr1["Par Name"].ToString();
dr1["point90"] = table3.Rows[5][para];
dr1["point10"] = table3.Rows[2][para];
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900