[英]Compare Row of 1st Datatable with Column of 2nd Datatable and build 3rd datatable with matched columns
http://www.codeproject.com/Questions/733071/Compare-Row-of-1st-Datatable-with-Column-of-2nd-Da (Understand the question) THESE ARE MY TWO DATATABLES (visual c#) http://www.codeproject.com/Questions/733071/Compare-Row-of-1st-Datatable-with-Column-of-2nd-Da (理解问题)这些是我的两个数据表(Visual C#)
table1
|Par Name.........| Par #|.......Units |.......LSL | USL | -----SKIP |
Diffusion.........908513100.......-.......... 0 -----99.9 -----1
Program...........908514100.......-......... 99.5--- 999 -----
table2
starttime | Product | Device | Diffusion | Program |
11/7/2013 SAF5100EL 163 -0.145712003 -0.146583006
11/7/2013 SAF5100EL 84 -0.137499005 -0.137592003
11/7/2013 SAF5100EL 44 -0.142690003 -0.143250003
11/7/2013 SAF5100EL 164 -0.139434993 -0.140459001
11/7/2013 SAF5100EL 34 -0.147183999 -0.148519993
output should look like 输出应该看起来像
table3
|Diffusion| | Program |
-0.145712003 -0.146583006
-0.137499005 -0.137592003
-0.142690003 -0.143250003
-0.139434993 -0.140459001
-0.147183999 -0.148519993
here the columns in table2 which match the row names of table1(for instance here diffusion and program) have to be fetched from datatable2 and new DataTable3 has to be created and with sorted values of columns (Diffusion and Program) 在这里,必须从datatable2中获取与table1的行名匹配的table2中的列(例如,此处的扩散和程序),并且必须创建新的DataTable3并使用列的排序值(Diffusion和Program)
As others have said, this is straight forward logic and that you should try it yourself first. 正如其他人所说,这是直截了当的逻辑,您应该首先自己尝试。 This will give you confidence as well.
这也会给您信心。
Anyways, the following code should do the trick. 无论如何,下面的代码应该可以解决问题。 Tweek it as per your convenience.
Tweek,根据您的方便。 Also test it thoroughly.
还要彻底测试。
//dt1 contains Diffusion etc as rows
//dt2 contains diffusion etc as columns
//dt3 is the required table
DataTable dt3 = new DataTable();
DataRow dr = null;
for (int i = 0; i < dt1.Rows.Count; i++)
{
string col = dt1.Rows[i]["Par Name"].ToString();
if (dt2.Columns.Contains(col))
{
if (!dt3.Columns.Contains(col))
{
dt3.Columns.Add(col, typeof(string));
}
if (dt3.Rows.Count == 0)
{
for (int j = 0; j < dt2.Rows.Count; j++)
{
dr = dt3.NewRow();
dt3.Rows.Add(dr);
}
}
for (int j = 0; j < dt2.Rows.Count; j++)
{
dt3.Rows[j][col] = dt2.Rows[j][col].ToString();
}
}
}
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.