是否可以遍历gridview(与数据库绑定)中的每个变量以从gridview获取总计?
<asp:TemplateField HeaderText="Unit Price">
<ItemTemplate>
<asp:Label ID="UnitPriceField" runat="server" Text='<%# Eval("ProductPrice")%>'>
</asp:Label>
</ItemTemplate>
GridViewRows
使用 Linq循环的简单方法:
double total = gridView1.Rows.Cast<GridViewRow>()
.Sum(r => double.Parse(((Label)r.FindControl("UnitPriceField")).Text));
但是,请注意,在数据库中而不是在 GUI 中计算这些东西总是更好。
RowDataBound
可以做你需要的,有很多例子: