如何从asp中减去两个colomn值。 net C#窗口表格,在不同的表格中

时间:2016-11-09 13:49:20

标签: asp.net

第一张表:

Table Fee_Paid

S.No  RollNo     Amount
1     14ERDME047  20000
2     14ERDME047  30000
3     14ERDME047  20000
4     15ERLME006  25000
5     15ERLME006  5000
6     16ERLCE025  25000

第二张表

表费用_Decide

S.No  RollNo     Amount
1    14ERDME047  70000
2    15ERLME006  50000
3    16ERLCE025  40000

我想要结果如下。

S.No  RollNo     Due_Amount
1    14ERDME047  0
2    15ERLME006  20000
3    16ERLCE025  15000

将第一个表项的添加与第二个表进行比较,并将被删除。

1 个答案:

答案 0 :(得分:0)

尝试下面的内容;​​

当按钮点击时,将计算两个表的方差。

<强> ASPX.cs

   //When the button clicks this will calculate the variance of two tables
    protected void LoadData(object sender,EventArgs e)
    {
        DataTable feePaid = new DataTable();
        SetColumns(feePaid);

        DataTable feeDecide = new DataTable();
        SetColumns(feeDecide);

        int feePaidRowCount = Fee_Paid.Rows.Count;
        int count = 0;
        int decideRowCount = Fee_Decide.Rows.Count;


        //Assign ASP.Net table data to Data Table
        for (int i = 1; i < feePaidRowCount; i++)
        {
            feePaid.Rows.Add(Fee_Paid.Rows[i].Cells[0].Text, Fee_Paid.Rows[i].Cells[1].Text, Fee_Paid.Rows[i].Cells[2].Text);
        }


        //Assign ASP.Net table data to Data Table
        for (int i = 1; i < decideRowCount; i++)
        {
            feeDecide.Rows.Add(Fee_Decide.Rows[i].Cells[0].Text, Fee_Decide.Rows[i].Cells[1].Text, Fee_Decide.Rows[i].Cells[2].Text);
        }


        foreach (DataRow one in feeDecide.Rows)
        {
            var rollNumber = one.Field<string>("RollNo");
            var decideAmount = Convert.ToInt32(one.Field<object>("Amount"));

            //Get the sum for a given roll number
            int paidAmount = feePaid.AsEnumerable().Where(a => a.Field<string>("RollNo").Equals(rollNumber)).Sum(c => c.Field<int>("Amount"));
            count++;

            if (paidAmount != 0)
            {
                var resultedAmount = decideAmount - paidAmount;
                TableRow r = new TableRow();
                TableCell numberCell = new TableCell();
                numberCell.Text = (count).ToString();
                TableCell rollCell = new TableCell();
                rollCell.Text = rollNumber;
                TableCell amountCell = new TableCell();
                amountCell.Text = resultedAmount.ToString();
                r.Cells.Add(numberCell);
                r.Cells.Add(rollCell);
                r.Cells.Add(amountCell);
                Result.Rows.Add(r); //Add the resultant to the result table
            }
        }
    }

    private static void SetColumns(DataTable tableName)
    {
        tableName.Columns.Add("S.No", typeof(int));
        tableName.Columns.Add("RollNo", typeof(string));
        tableName.Columns.Add("Amount", typeof(int));
    }

<强> ASPX

    <asp:Table ID="Fee_Paid" runat="server">

    <asp:TableHeaderRow>
        <asp:TableHeaderCell>
            S.No
        </asp:TableHeaderCell>
           <asp:TableHeaderCell>
           RollNo
        </asp:TableHeaderCell>
         <asp:TableHeaderCell>
           Amount
        </asp:TableHeaderCell>
    </asp:TableHeaderRow>
    <asp:TableRow>
        <asp:TableCell>
            1
        </asp:TableCell>
        <asp:TableCell>
            14ERDME047
        </asp:TableCell>
        <asp:TableCell>
            20000
        </asp:TableCell>
    </asp:TableRow>

                <asp:TableRow>
        <asp:TableCell>
            2
        </asp:TableCell>
        <asp:TableCell>
            14ERDME047
        </asp:TableCell>
        <asp:TableCell>
            30000
        </asp:TableCell>
    </asp:TableRow>

                <asp:TableRow>
        <asp:TableCell>
            3
        </asp:TableCell>
        <asp:TableCell>
            14ERDME047
        </asp:TableCell>
        <asp:TableCell>
            20000
        </asp:TableCell>
    </asp:TableRow>

                <asp:TableRow>
        <asp:TableCell>
            4
        </asp:TableCell>
        <asp:TableCell>
            15ERLME006
        </asp:TableCell>
        <asp:TableCell>
            25000
        </asp:TableCell>
    </asp:TableRow>

                <asp:TableRow>
        <asp:TableCell>
            5
        </asp:TableCell>
        <asp:TableCell>
            15ERLME006
        </asp:TableCell>
        <asp:TableCell>
            5000
        </asp:TableCell>
    </asp:TableRow>

                <asp:TableRow>
        <asp:TableCell>
            6
        </asp:TableCell>
        <asp:TableCell>
            16ERLCE025
        </asp:TableCell>
        <asp:TableCell>
            25000
        </asp:TableCell>
    </asp:TableRow>
</asp:Table>

<asp:Table ID="Fee_Decide" runat="server">
                <asp:TableHeaderRow>
        <asp:TableHeaderCell>
            S.No
        </asp:TableHeaderCell>
           <asp:TableHeaderCell>
           RollNo
        </asp:TableHeaderCell>
         <asp:TableHeaderCell>
           Amount
        </asp:TableHeaderCell>
    </asp:TableHeaderRow>
    <asp:TableRow>
        <asp:TableCell>
            1
        </asp:TableCell>
        <asp:TableCell>
            14ERDME047
        </asp:TableCell>
        <asp:TableCell>
            70000
        </asp:TableCell>
    </asp:TableRow>

                <asp:TableRow>
        <asp:TableCell>
            2
        </asp:TableCell>
        <asp:TableCell>
            15ERLME006
        </asp:TableCell>
        <asp:TableCell>
            50000
        </asp:TableCell>
    </asp:TableRow>

                <asp:TableRow>
        <asp:TableCell>
            3
        </asp:TableCell>
        <asp:TableCell>
            16ERLCE025
        </asp:TableCell>
        <asp:TableCell>
            40000
        </asp:TableCell>
    </asp:TableRow>



</asp:Table>
<asp:Table ID="Result" runat="server">

                            <asp:TableHeaderRow>
        <asp:TableHeaderCell>
            S.No
        </asp:TableHeaderCell>
           <asp:TableHeaderCell>
           RollNo
        </asp:TableHeaderCell>
         <asp:TableHeaderCell>
           Amount
        </asp:TableHeaderCell>
    </asp:TableHeaderRow>
</asp:Table>


<asp:Button ID="Button1" runat="server" Text="Calculate Variance" OnClick="LoadData" />
  • 获取具有相同卷号的Fee_Paid数据行,并计算“金额”的总和。
  • 然后从Fee_Decide的“金额”值中扣除总和 相同的卷号并将其添加到结果表中。