Gridview页脚计算

时间:2018-03-29 11:24:26

标签: c# asp.net sql-server gridview

我在页面上有5个GridView。所有这些都显示不同的表数据,并显示页脚中的总计算。

但我需要计算所有GridView中的所有数据并将其显示在Label中。

[Screenshot of the GridViews]

1 个答案:

答案 0 :(得分:0)

使用OnRow Created ..

在顶部声明变量
双重计数器;

double GrandTotalCounter;

这是示例代码引用并在您的内容中进行更改..

    protected void OnRowCreated(object sender, GridViewRowEventArgs e)
    {

        bool IsGrandTotalRowNeedtoAdd = false;
          if ((strPreviousRowID != string.Empty) && (DataBinder.Eval(e.Row.DataItem, "city_name") == null))
        {


            IsGrandTotalRowNeedtoAdd = true;
            intTotalIndex = 0;
        }
        if (IsGrandTotalRowNeedtoAdd)
        {
            GridView grdViewProducts = (GridView)sender;
            GridViewRow GrandTotalRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
            GrandTotalRow.Font.Bold = true;
            GrandTotalRow.BackColor = System.Drawing.Color.LightPink;
            GrandTotalRow.ForeColor = System.Drawing.Color.White;

            TableCell HeaderCell = new TableCell();
            HeaderCell.Text = "Grand Total";
            HeaderCell.HorizontalAlign = HorizontalAlign.Left;
            HeaderCell.ColumnSpan = 3;
            HeaderCell.Font.Bold = true;
            HeaderCell.ForeColor = System.Drawing.Color.Red;
            HeaderCell.Font.Size = 10;
            GrandTotalRow.Cells.Add(HeaderCell);
           // you can use how many fields you want to calculate
            HeaderCell = new TableCell();
            HeaderCell.Text = string.Format("{0:0.00}", GrandTotalCounter);
            HeaderCell.HorizontalAlign = HorizontalAlign.Right;
            HeaderCell.Font.Bold = true;
            HeaderCell.ForeColor = System.Drawing.Color.Red;
            HeaderCell.Font.Size = 10;
            GrandTotalRow.Cells.Add(HeaderCell);
            //////////////
            GrandTotalRow.Cells.Add(HeaderCell);
            grdViewProducts.Controls[0].Controls.AddAt(e.Row.RowIndex, 
          GrandTotalRow);
        }}

on RowDataBound

   protected void RowDataBound(object sender, GridViewRowEventArgs e)
    {
        strtotalID = DataBinder.Eval(e.Row.DataItem, "city_name").ToString();
                double TtCounter = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "CounterAmt").ToString());


                TotalCounter += TtCounter;


                GrandTotalCounter += TCounter;
             /// Add how much you want 
          }