//
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就是你想要的列了
}
}