【问题标题】:How to do calculation in dynamic gridview?如何在动态gridview中进行计算?
【发布时间】:2019-11-29 07:58:23
【问题描述】:

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

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        |
------------------------------------------------

感谢您的帮助.. 谢谢

【问题讨论】:

    标签: asp.net gridview


    【解决方案1】:

    试试这个:

    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();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      相关资源
      最近更新 更多