GridView1 footer求和
//
ShowFooter="True"
private double sum = 0;//取指定列的数据和,你要根据具体情况对待可能你要处理的是int
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToDouble(e.Row.Cells[5].Text)
}
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToDouble(e.Row.Cells[5].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[4].Text = "总价格为:";
e.Row.Cells[5].Text = sum.ToString();
e.Row.Cells[2].Text = "平均价格为:";
e.Row.Cells[3].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
}
}
1、先在顶部声明公共变量
private float production_Cost = 0;
2、然后将Gridview的【ShowFooter】属性设置为【true】。
3、再在Gridview的【RowDataBound】事件中写道:
protected void Gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
production_Cost += float.Parse(drv["totalCosts"].ToString());
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[11].Text = "小計:";
//e.Row.Cells[12].Text = Math.Round(production_Cost, 5).ToString();
e.Row.Cells[12].Text = production_Cost.ToString();
}
}
if(e.Row.RowType==DataControlRowType.Footer)
{
GridViewRow headrow = GridView1.HeaderRow;
GridViewRow footerrow = GridView1.FooterRow;
for (int j = 0; j < headrow.Cells.Count - 1; j++)
{
string headfield = headrow.Cells[j].Text;
foreach (string b in All_Field)
{
if (headfield.Contains(b))
{
string sumnum = dssumresult.Tables[0].Rows[0][headfield].ToString();
footerrow.Cells[j].Text = sumnum;
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
decimal dsum =0;
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
if (GridView1.HeaderRow.Cells[i].Text.IndexOf("(N)")>-1)
{
for (int j = 0; j < GridView1.Rows.Count; j++)
{
dsum += Convert.ToDecimal(GridView1.Rows[j].Cells[i].Text.Trim());
}
GridView1.FooterRow.Cells[i].Text = dsum.ToString();
}
//if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
//{
// string name = GridView1.Rows[i].Cells[0].Text;//获取第i行第1列的内容
// TextBox txtID = (TextBox)GridView1.Rows[i].Cells[1].FindControl("textBox");
// string ID = txtID.Text; //获取TextBox里的内容
//}
}
}
//创建DataTable
protected DataTable CreateDT()
{
DataTable tblDatas = new DataTable("Datas");
//序号列
//tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
//tblDatas.Columns[0].AutoIncrement = true;
//tblDatas.Columns[0].AutoIncrementSeed = 1;
//tblDatas.Columns[0].AutoIncrementStep = 1;
//数据列
tblDatas.Columns.Add("身份证号码", Type.GetType("System.String"));
tblDatas.Columns.Add("姓名", Type.GetType("System.String"));
tblDatas.Columns.Add("出生日期", Type.GetType("System.String"));
tblDatas.Columns.Add("性别", Type.GetType("System.String"));
tblDatas.Columns.Add("基本工资", Type.GetType("System.Decimal"));
tblDatas.Columns.Add("奖金", Type.GetType("System.Decimal"));
//统计列开始
tblDatas.Columns.Add("应发合计", Type.GetType("System.String"), "基本工资+奖金");
//统计列结束
tblDatas.Columns.Add("家庭住址", Type.GetType("System.String"));
tblDatas.Columns.Add("邮政编码", Type.GetType("System.String"));
tblDatas.Rows.Add(new object[] { null, "张三", "1982", "0", 3000, 1000, null, "深圳市", "518000" });
tblDatas.Rows.Add(new object[] { null, "李四", "1983", "1", 3500, 1200, null, "深圳市", "518000" });
tblDatas.Rows.Add(new object[] { null, "王五", "1984", "1", 4000, 1300, null, "深圳市", "518000" });
tblDatas.Rows.Add(new object[] { null, "赵六", "1985", "0", 5000, 1400, null, "深圳市", "518000" });
tblDatas.Rows.Add(new object[] { null, "牛七", "1986", "1", 6000, 1500, null, "深圳市", "518000" });
return tblDatas;
}
//转换DataTable中的数据 用于逻辑处理相应的数据 显示
protected DataTable FormatDT()
{
DataTable dt1 = CreateDT();
//容错处理 用于不确定 自动产生的列
if (dt1.Columns.Contains("性别"))
{
foreach (DataRow dr in dt1.Rows)
{
dr["性别"] = (dr["性别"].ToString() == "0") ? "女" : "男";
}
}
return dt1;
}
/// <summary>
/// 将DT转换为Execl的方法
/// </summary>
/// <param name="dt">需要导出的DT
/// <param name="page">页面
/// <param name="fileName">文件名
public void ToExecl(DataTable dt, Page page, string fileName)
{
HttpResponse response = page.Response;
response.Clear();
response.ContentType = "application/x-excel";
response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
StringBuilder sB = new StringBuilder();
for (int j = 0; j < dt.Columns.Count; j++)
{
sB.Append(dt.Columns[j].Caption + "\t");
}
sB.Append("\n");
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int k = 0; k < dt.Columns.Count; k++)
{
sB.Append("=\"" + dt.Rows[i][k].ToString() + "\"\t"); //解决导出的单元格以科学计数法显示的问题
}
sB.Append("\n");
}
response.Write(sB.ToString());
response.End();
}
public void ToWord(DataTable dt, Page page, string filName)
{
HttpResponse response = page.Response;
response.Clear();
response.ContentType = "application/msword";
response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
response.AddHeader("Content-Disposition","attachment:filename="+System.Web.HttpUtility.UrlEncode(filName,System.Text.Encoding.UTF8)+".doc");
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < dt.Rows.Count; i++)
{
sBuilder.Append(dt.Rows[i][1].ToString()+"\n");
}
response.Write(sBuilder.ToString());
response.End();
}
public void ToXML(DataTable dt, Page page, string filename)
{
HttpResponse response = page.Response;
//DataSet ds = new DataSet();
response.Clear();
response.ContentType = "application/x-excel";
response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename,System.Text.Encoding.UTF8) + ".xls");
System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();
System.Xml.XmlTextWriter xw = new XmlTextWriter(response.OutputStream, utf8);
xw.Formatting = Formatting.Indented;
xw.Indentation = 4;
xw.IndentChar = ' ';
dt.TableName = "dd";
dt.WriteXml(xw);
dt = null;
GC.Collect();
xw.Flush();
xw.Close();
response.End();
}
foreach (GridViewRow gvr in GridView1.Rows)
{
foreach (TableCell gvCell in gvr.Cells)
{
//gvCell就是你想要的列了
}
}
GridView1 footer求和的更多相关文章
- Gridview中实现求和统计功能
GridView加入自动求和求平均值小计 效果图: 解决方案: private double sum = 0; //取指定列的数据和,你要根据具体情况对待可能你要处理的是int protected v ...
- Datatable中对某列求和,三种不同情况下的方法
C# code 方法一. object sumObject = DataTable.Compute("sum(Qty)", "TRUE"); 直接对数据表中的字 ...
- Java程序:从命令行接收多个数字,求和并输出结果
一.设计思想:由于命令行接收的是字符串类型,因此应先将字符串类型转化为整型或其他字符型,然后利用for循环求和并输出结果 二.程序流程图: 三.源程序代码: //王荣荣 2016/9/23 ...
- Java之递归求和的两张方法
方法一: package com.smbea.demo; public class Student { private int sum = 0; /** * 递归求和 * @param num */ ...
- EXCEL中对1个单元格中多个数字求和
如A1=3779.3759.3769.3781.3750,A2对A1中4个数字求和怎么求!请高手赐教! 方法一:在B1中输入公式=SUM(MID(A1,{1,6,11,16,21},4)*1) 方法二 ...
- codevs 1082 线段树区间求和
codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...
- 一个将 footer 保持在底部的最好方法
原文: Quick Tip: The Best Way To Make Sticky Footers 当你在布局网页时,有可能会遇到类似下面的这种情况 导致这一问题的原因是页面内容太少,无法将内容区域 ...
- 从sum()求和引发的思考
sum()求和是一个非常简单的函数,以前我的写法是这样,我想大部分和我一样刚开始学习JS的同学写出来的也会是这样. function sum() { var total=null; for(var i ...
- //给定N个整数序列{A1,A2,A3...An},求函数f(i,j)=(k=i~j)Ak的求和
//给定N个整数序列{A1,A2,A3...An},求函数f(i,j)=(k=i~j)Ak的求和 # include<stdio.h> void main() { ,sum1; ]={,- ...
随机推荐
- Oracle DBA的学习(笔记)
1.软件任务分组:系统分析师.系统设计师.程序员.测试人员.开发dba.系统架构师.实施人员. 2.常用设计图:活动图.用例图.类图.序列图. 2010-9-15(dba学习) 1.1 Oracle产 ...
- 【题解】Zap(莫比乌斯反演)
[题解]Zap(莫比乌斯反演) 裸题... 直接化吧 [P3455 POI2007]ZAP-Queries 所有除法默认向下取整 \[ \Sigma_{i=1}^x\Sigma_{j=1}^y[(i, ...
- MySQL中事务的分类
从事务理论的角度来看,可以把事务分为以下几种类型 扁平事务(Flat Transactions) 带有保存点的扁平事务(Flat Transactions with Savepoints) 链事务(C ...
- ubuntu常见错误--Could not get lock /var/lib/dpkg/lock解决(转)
通过终端安装程序sudo apt-get install xxx时出错: E: Could not get lock /var/lib/dpkg/lock - open (11: Resource t ...
- java bio 之聊天室
最近在复习java io相关知识 ,发现很多细节之前没学习到位,原理也没吃透,只能感叹本人愚钝. 复习到bio,顺手写了个简单的聊天室功能,并和大家分享下. 服务端: package io.QQ聊天室 ...
- php......留言板
部门内部留言板 一.语言和环境 实现语言 PHP 二.要求: 本软件是作为部门内员工之间留言及发送消息使用. 系统必须通过口令验证,登录进入.方法是从数据库内取出用户姓名和口令的数据进行校验. 用户管 ...
- VC6.0 开发 64 位程序
1. 设置平台SDK(如:Microsoft platform sdk 2003),选择64位的编译.链接环境. setenv /XP64 /DEBUG 2. 利用这个环境启动VC6.0. msdev ...
- jquery click()方法模拟点击事件对a标签不生效
if(e.keyCode == 13) { $items.eq(index).click(); return; } 搜索框下拉列表模拟点击时间,使用上述代码不能触发链接跳转 1,页面使用了bootst ...
- 结合canvas做雨滴特效
雨滴特效 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
- OC_NSString、
8月17日NSString 一.字符串的创建 //1.创建常量字符串 NSString *str1 = @"hello world"; NSLog(@"%@", ...