如何在动态网格视图中进行计算?

How to do calculation in dynamic gridview?

我有以下基于批记录数创建的动态网格视图

aspx.cs

batchNo = Request.QueryString["numberOfbatch"];

for (int i= 0, i<= batchNo.Split(',').Length - 1, i++)
{
    GridView gv = new GridView();
    BoundField BatchID = new BoundField();
    BatchID.DataField = "BatchID";
    BatchID.HeaderText = "Batch ID";
    BoundField BatchDate = new BoundField();
    BatchDate.DataField = "BatchDate";
    BatchDate.HeaderText = "Batch Date";
    BoundField BatchAmount = new BoundField();
    BatchAmount.DataField = "BatchAmount";
    BatchAmount.HeaderText = "Batch Amount";

    lbTitle.Text = "Batch No: " + batchNo[i];

    gv.AutoGenerateColumns = false;
    gv.EmptyDataText = "No batch data.";

    // query goes here
    sSql = "select * from batchTable where BatchID = "+ batchNo.Split(',')[i];

    gv.Columns.Add(BatchID);
    gv.Columns.Add(BatchDate);
    gv.Columns.Add(BatchAmount);

    gv.DataSource = db.returnDataSet(sSql);
    gv.DataBind();

    phGridView.Controls.Add(new LiteralControl("<br />"));
    phGridView.Controls.Add(lbTitle);
    phGridView.Controls.Add(gv);

}

到目前为止,我得到了我需要的输出如下:

Batch No : 1234
------------------------------------------------
| Batch ID    | Batch Date    | Batch Amount   |
------------------------------------------------
| A1          | 29/11/2019    | 1000.00        |
| A2          | 29/11/2019    |  500.00        |
------------------------------------------------

Batch No : 2222
------------------------------------------------
| Batch ID    | Batch Date    | Batch Amount   |
------------------------------------------------
| C1          | 29/11/2019    | 1500.00        |
| D2          | 29/11/2019    |  800.00        |
------------------------------------------------

Batch No : 3333
------------------------------------------------
| Batch ID    | Batch Date    | Batch Amount   |
------------------------------------------------
| Z1          | 29/11/2019    | 2000.00        |
| Z2          | 29/11/2019    |  100.00        |
| Z3          | 29/11/2019    |  800.00        |
| Z4          | 29/11/2019    | 2100.00        |
------------------------------------------------

我的问题是..我如何计算每批的总金额..我想在页脚添加以计算总金额..例如如下:

Batch No : 3333
------------------------------------------------
| Batch ID    | Batch Date    | Batch Amount   |
------------------------------------------------
| Z1          | 29/11/2019    | 2000.00        |
| Z2          | 29/11/2019    |  100.00        |
| Z3          | 29/11/2019    |  800.00        |
| Z4          | 29/11/2019    | 2100.00        |
------------------------------------------------
|             |               | 5000.00        |
------------------------------------------------

感谢您的帮助..谢谢

试试这个:

double total = 0;
        for (int i = 0; i <= batchNo.Split(',').Length - 1; i++)
        {
            GridView gv = new GridView();
            BoundField BatchID = new BoundField();
            BatchID.DataField = "BatchID";
            BatchID.HeaderText = "Batch ID";
            BoundField BatchDate = new BoundField();
            BatchDate.DataField = "BatchDate";
            BatchDate.HeaderText = "Batch Date";
            BoundField BatchAmount = new BoundField();
            BatchAmount.DataField = "BatchAmount";
            BatchAmount.HeaderText = "Batch Amount";
            Label lbTitle = new Label();

            lbTitle.Text = "Batch No: " + batchNo[i];

            gv.AutoGenerateColumns = false;
            gv.EmptyDataText = "No batch data.";

            // query goes here
            sSql = "select * from batchTable where BatchID = " + batchNo.Split(',')[i];

            gv.Columns.Add(BatchID);
            gv.Columns.Add(BatchDate);
            gv.Columns.Add(BatchAmount);
            gv.ShowFooter = true;

            gv.DataSource = db.returnDataSet(sSql);
            gv.DataBind();

            phGridView.Controls.Add(new LiteralControl("<br />"));
            phGridView.Controls.Add(lbTitle);
            phGridView.Controls.Add(gv);

            total = 0;

            foreach (GridViewRow gvr in gv.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    double val = 0.0;
                    try
                    {
                        val = Convert.ToDouble(gvr.Cells[2].Text);
                    }
                    catch (Exception ex) { }
                    total = val + total;
                }
            }

            gv.FooterRow.Cells[2].Text = total.ToString();
        }