前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何在GridView的Footer内显示总计?

如何在GridView的Footer内显示总计?

作者头像
菩提树下的杨过
发布2018-01-22 16:32:00
8940
发布2018-01-22 16:32:00
举报

前台:

代码语言:javascript
复制
<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>

后台:

代码语言:javascript
复制
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"是数据源的一个字段

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2007-11-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档