How to join DataTable

2019-06-07 15:51发布

T1 and T2 are DataTables with following fields

T1(SPEC, DWGNO, ITEM NAME, DESCRIPTION, SIZE, AMOUNT)

T2(SPEC, DWGNO, ITEM NAME, DESCRIPTION, SIZE, AMOUNT)

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

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

I need the join table

TJ (SPEC, DWGNO, ITEM NAME, DESCRIPTION, SIZE, 1st-2nd, 1st, 2nd)

How this can be done in C# code in a simple way? Thanks.

1条回答
我命由我不由天
2楼-- · 2019-06-07 16:08

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.

查看更多
登录 后发表回答