asp gridview 页脚ShowFooter绑定数据,增加一个统计行方法,ShowFooter统计行方法

本文介绍了如何在ASP Gridview中启用ShowFooter属性,并通过GridView1_RowDataBound事件动态绑定数据,实现统计行功能。在数据绑定过程中,对DataRowView类型的数据进行操作,累加特定列的值。在处理footer行时,更新对应单元格的总计信息,展示统计数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先设置gridview的ShowFooter="True" ,每次gridview读取绑定数据都会出发gridview中的GridView1_RowDataBound事件,从而得到每一行的数据。

第一种情况。gridview动态绑定数据。

 aspx

<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("shuju") %>'></asp:Label>//shuju为自己绑定数据库中
</ItemTemplate>
<FooterTemplate>
    <asp:Label id="labelAll" runat="server" Text="总计:"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
cs中
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["shuju"]);//行标题
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
       Label LabelAll =  e.Row.FindControl("labelAll") as Label;
       if (LabelAll != null)
       {
           LabelAll.Text += num.ToString();//"计算的总数,或者也可以单独计算";//
       }
    }
}
第二种情况grid没有绑定数据
aspx中
     <asp:GridView ShowFooter="True"  ID="GridView2" runat="server" BackColor="White"
            BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"
            ForeColor="Black" GridLines="Vertical" style="margin-left: 0px"
            onrowdatabound="GridView2_RowDataBound"  >
            <AlternatingRowStyle BackColor="#CCCCCC" />  </asp:GridView>
cs中

    double  num = 0;
    double i = 0;
    double numfz = 0;
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {

         if (e.Row.RowType == DataControlRowType.DataRow)   
         {     
             DataRowView dr = e.Row.DataItem as DataRowView;   
           num += Convert.ToDouble(dr.Row["bidui"]);//数据库中有数据为12.3,则用ToDouble,比对为表列名称,获得其中内容
           numfz += Convert.ToDouble(dr.Row["fuzhi"]);
             i++;
         }  
         else if (e.Row.RowType == DataControlRowType.Footer)   //绑定到最后一行时候,RowType 为DataControlRowType.Footer
         {  
             //Label LabelAll =  e.Row.FindControl("labelAll") as Label;  
             //if (LabelAll != null)    
             //{      
             //    LabelAll.Text += num.ToString();//"计算的总数,或者也可以单独计算";//  
             //}
           e.Row.Cells[0].Text = "总计: "+i+"条"; //这是最后一行第一个单元格里面内容信息
            e.Row.Cells[12].Text = "总计: " + num;
           e.Row.Cells[13].Text = "总计: " + numfz ;
  

         }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值