1

是否可以遍历gridview(与数据库绑定)中的每个变量以从gridview获取总计?

<asp:TemplateField HeaderText="Unit Price">
<ItemTemplate>
          <asp:Label ID="UnitPriceField" runat="server" Text='<%# Eval("ProductPrice")%>'>             
          </asp:Label>
</ItemTemplate>
4

2 回答 2

2

GridViewRows使用 Linq循环的简单方法:

double total = gridView1.Rows.Cast<GridViewRow>()
          .Sum(r => double.Parse(((Label)r.FindControl("UnitPriceField")).Text));

但是,请注意,在数据库中而不是在 GUI 中计算这些东西总是更好。

于 2013-01-08T13:23:49.547 回答
0

RowDataBound可以做你需要的,有很多例子:

ASP.NET 2.0 的 GridView 示例:在页脚中显示摘要数据

于 2013-01-08T13:19:57.013 回答