【发布时间】:2018-09-08 08:09:33
【问题描述】:
我在一个页面上有 5 个 GridView。它们都显示不同的表格数据,并在页脚显示总计的计算。
但我需要计算所有 GridView 中的所有数据并将其显示在标签中。
[]
【问题讨论】:
标签: c# asp.net sql-server gridview
我在一个页面上有 5 个 GridView。它们都显示不同的表格数据,并在页脚显示总计的计算。
但我需要计算所有 GridView 中的所有数据并将其显示在标签中。
[]
【问题讨论】:
标签: c# asp.net sql-server gridview
使用 OnRow Created..
在顶部声明变量
双计数器;
双 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);
}}
在 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
}
【讨论】: