继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

如何在GridView的Footer内显示总计

蛊毒传说
关注TA
已关注
手记 65
粉丝 17
获赞 83

前台:

<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"是数据源的一个字段

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP