GridView添加统计(合计)行

前提:设置属性ShowFooter="True"

方法一:使用SQL查询统计出合计值,在绑定GridView时让其结果赋于一个DataTable(全局变量),然后在RowDataBound事件中

None.gif if  (e.Row.RowType  ==  DataControlRowType.Footer)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            e.Row.Cells[
0].Text = "合计";
InBlock.gif            e.Row.Cells[
3].Text = dtSum.Rows[0][0].ToString();
InBlock.gif            e.Row.Cells[
4].Text = dtSum.Rows[0][1].ToString();
InBlock.gif            e.Row.Cells[
5].Text = dtSum.Rows[0][2].ToString();
InBlock.gif            e.Row.Cells[
6].Text = dtSum.Rows[0][3].ToString();
InBlock.gif            e.Row.Cells[
7].Text = dtSum.Rows[0][4].ToString();
InBlock.gif            e.Row.Cells[
8].Text = dtSum.Rows[0][5].ToString();
InBlock.gif            e.Row.Cells[
9].Text = dtSum.Rows[0][6].ToString();
InBlock.gif            e.Row.Cells[
10].Text = dtSum.Rows[0][7].ToString();
InBlock.gif            e.Row.Cells[
11].Text = dtSum.Rows[0][8].ToString();
ExpandedBlockEnd.gif        }

其中dtSum是那个全局DataTable,在绑定GridView同时将SQL查询的结果赋给它;效果如下:


方法二、直接把对应列每一行的值相加(不做数据查询,在RowDataBound事件中运算)

None.gif    int  mysum1  =   0 ;
None.gif    
int  mysum2  =   0 ;
None.gif    
protected   void  GridList_RowDataBound( object  sender, GridViewRowEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
if (e.Row.RowType == DataControlRowType.DataRow )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataRowView myrows
=(DataRowView)e.Row.DataItem;
InBlock.gif            mysum1 
+=Convert .ToInt32 (myrows[2].ToString ());
InBlock.gif            mysum2 
+= Convert.ToInt32(myrows[3].ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif        
// 合计
InBlock.gif
        if (e.Row.RowType == DataControlRowType.Footer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            e.Row.Cells[
0].Text = "合计";
InBlock.gif            e.Row.Cells[
1].Text = mysum1.ToString();
InBlock.gif            e.Row.Cells[
2].Text = mysum2.ToString();
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值