c# - 如何在gridview中求和从asp.net数据库中选择数据

标签 c# asp.net gridview

我在 asp.net 工作,我有一个 GridView ,在其中我可以从基本工资中找到 pf 和 ESI 值,我想要在 Repues 的 gridview 页脚中正确解决总 PF 和总 ESI 的总和,所以请帮助我

最佳答案

你必须在gridview的RowDataBound事件中编写代码。

我发布了一个示例代码供您引用,您自己尝试一下

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      Label lblPrice = (Label)e.Row.FindControl("lblPrice");
      Label lblUnitsInStock = (Label)e.Row.FindControl("lblUnitsInStock");

      decimal price = Decimal.Parse(lblPrice.Text);
      decimal stock = Decimal.Parse(lblUnitsInStock.Text);

      totalPrice += price;
      totalStock += stock;

      totalItems += 1;
   }

   if (e.Row.RowType == DataControlRowType.Footer)
   {
      Label lblTotalPrice = (Label)e.Row.FindControl("lblTotalPrice");
      Label lblTotalUnitsInStock = (Label)e.Row.FindControl("lblTotalUnitsInStock");

      lblTotalPrice.Text = totalPrice.ToString();
      lblTotalUnitsInStock.Text = totalStock.ToString();

      lblAveragePrice.Text = (totalPrice / totalItems).ToString("F");
   }
}

也检查一下 http://msdn.microsoft.com/en-us/library/ms972833.aspx

关于c# - 如何在gridview中求和从asp.net数据库中选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16456036/

相关文章:

c# - 将带有指针的 C++ DLL 导入 C#

asp.net - asp :RegularExpressionValidator, 确保字符串中没有逗号

c# - 在回发期间 JavaScript 失去其功能

ASP.NET Razor Html.TextArea

gridview - extjs 有状态网格删除排序

c# - sql数据源重新绑定(bind)问题

android - 覆盖到 GridView 上

c# - 使用属性扩展密封类

c# - 此 BackgroundWorker 当前正忙,无法同时运行多个任务

c# - 在设置先决条件导入之前无法调用 GetExportedValue