asp.net在Datatable中对数据求和,但在第1行和最后一行中缺少数据

时间:2018-06-01 03:36:35

标签: c# asp.net

我有一个从数据库获取数据的表单(商品名,商品名,价格等),将它们添加到数据表中,计算并返回一些新列(totalamount,taxamount),然后汇总新列和显示文本框返回的值。

我的问题是当我第一次添加新行时,我的文本框什么都没显示。并且第二次及以上,它不是最后一行的总和。我认为我的循环中出现了问题。 我怎样才能解决这个问题?谢谢!

首次添加行

enter image description here

第二次及以上

enter image description here

这是我的代码:

2

1 个答案:

答案 0 :(得分:0)

我明白了。不是计算数据表中的行,而是每次插入一行时,我都会从gridview中对列进行求和,这对我有用!谢谢!

    protected void CommodityList_RowDataBound(object sender, GridViewRowEventArgs e)
{
    decimal totalamount = 0;
    decimal taxamount = 0;
    for (int i = 0; i < CommodityList.Rows.Count; i++)
    {
        String total1 = (CommodityList.Rows[i].Cells[5].Text.Trim());
        totalamount += decimal.Parse(total1);
        String total2 = (CommodityList.Rows[i].Cells[6].Text.Trim());
        taxamount += decimal.Parse(total2);
        totalpayment = totalamount + taxamount;
    }
    txt_TotalAmount.Text = totalamount.ToString();
    txt_TotalTax.Text = taxamount.ToString();
    txt_TotalPayment.Text = totalpayment.ToString();
}