如何加入数据表 [英] How to join DataTable
本文介绍了如何加入数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
T1和T2是具有以下字段的数据表
T1 and T2 are DataTables with following fields
T1(SPEC, DWGNO, ITEM NAME, DESCRIPTION, SIZE, AMOUNT)
T2(SPEC, DWGNO, ITEM NAME, DESCRIPTION, SIZE, AMOUNT)
如果SPEC,DWGNO,项目名称,ESCRIPTION,SIZE相等
If SPEC, DWGNO, ITEM NAME, ESCRIPTION, SIZE are equal
AMOUNT = T1AMOUNT - T2AMOUNT;
New Column 1st(T1 AMOUNT), 2nd(T2 AMOUNT)
and T1 Amount add to 1st, T2 Amount add to 2nd
如果SPEC,DWGNO,项目名称,ESCRIPTION,SZIE不同,则将T1量添加到第一列,将T2量添加到第二列
IF SPEC, DWGNO, ITEM NAME, ESCRIPTION, SZIE is different Then T1 Amount is add to 1st column and T2 Amount is add to 2st column
我需要联接表
TJ (SPEC, DWGNO, ITEM NAME, DESCRIPTION, SIZE, 1st-2nd, 1st, 2nd)
如何使用简单的C#代码完成此操作办法?谢谢。
How this can be done in C# code in a simple way? Thanks.
推荐答案
这可能对您有帮助...
This may help you...
DataTable dt1 = new DataTable("Table1");
DataTable dt2 = new DataTable("Table2");
DataTable dt3 = new DataTable("Table3");
if (dt1.Rows[0]["A1"] == dt2.Rows[0]["A2"])
{
dt3.Rows.Add(dt1.Rows[0]["A1"].ToString(), dt1.Rows[0]["B1"].ToString(), dt1.Rows[0]["C1"].ToString(), Convert.ToInt32(dt1.Rows[0]["D1"]) + Convert.ToInt32(dt1.Rows[0]["D2"]));
}
您可以对表的所有行使用循环,并使用基本数学运算来计算列。
You can use loops for all the rows of the tables, and basic maths to calculate the columns.
这篇关于如何加入数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文