前台:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
OnRowDataBound="GridView1_RowDataBound"
ShowFooter="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="labelfirst" runat="server" Text='<%# Eval("first") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label id="labelAll" runat="server" Text="总计:"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
后台:
private int num = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView dr = e.Row.DataItem as DataRowView;
num += Convert.ToInt32(dr.Row["first"]);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
Label LabelAll = e.Row.FindControl("labelAll") as Label;
if (LabelAll != null)
{
LabelAll.Text += num.ToString();//"计算的总数,或者也可以单独计算";//
}
}
}
"first"是数据源的一个字段
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有