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


    前台:

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

    作者:菩提树下的杨过
    出处:http://yjmyzz.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Python之datetime模块
    PEP8规范 Python
    redis操作命令
    Django之Cookie、Session和自定义分页
    登录之验证码相关实现
    装饰器进阶
    js中的cookie使用和vue-cookie的使用
    vue-cli的安装使用
    Django之进阶相关操作
    PyMySQL模块的使用
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/977497.html
Copyright © 2020-2023  润新知