asp.net GridView 在报表底部增加合计行
2007-12-20 09:16
357 查看
首先:在GridView 属性设置中,ShowFooter 设为 true
方法一:使用SQL查询统计出合计值,在绑定GridView时让其结果赋于一个DataTable(全局变量),然后在RowDataBound事件中
if (e.Row.RowType == DataControlRowType.Footer)
...{
e.Row.Cells[0].Text = "合计";
e.Row.Cells[3].Text = dtSum.Rows[0][0].ToString();
e.Row.Cells[4].Text = dtSum.Rows[0][1].ToString();
e.Row.Cells[5].Text = dtSum.Rows[0][2].ToString();
e.Row.Cells[6].Text = dtSum.Rows[0][3].ToString();
e.Row.Cells[7].Text = dtSum.Rows[0][4].ToString();
e.Row.Cells[8].Text = dtSum.Rows[0][5].ToString();
e.Row.Cells[9].Text = dtSum.Rows[0][6].ToString();
e.Row.Cells[10].Text = dtSum.Rows[0][7].ToString();
e.Row.Cells[11].Text = dtSum.Rows[0][8].ToString();
}
其中dtSum是那个全局DataTable,在绑定GridView同时将SQL查询的结果赋给它;效果如下:
方法二、直接把对应列每一行的值相加(不做数据查询,在RowDataBound事件中运算)
int mysum1 = 0;
int mysum2 = 0;
protected void GridList_RowDataBound(object sender, GridViewRowEventArgs e)
...{
if (e.Row.RowType == DataControlRowType.DataRow )
...{
DataRowView myrows=(DataRowView)e.Row.DataItem;
mysum1 +=Convert .ToInt32 (myrows[2].ToString ());
mysum2 += Convert.ToInt32(myrows[3].ToString());
}
// 合计
if (e.Row.RowType == DataControlRowType.Footer)
...{
e.Row.Cells[0].Text = "合计";
e.Row.Cells[1].Text = mysum1.ToString();
e.Row.Cells[2].Text = mysum2.ToString();
}
}
方法三:
1. 在Web Form 的源文件中,修改GridView部分,添加 OnRowDataBound 响应函数 OnRowDataBound = "GridView1_RowDataBound"
2. 在代码页,添加该函数代码如下:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRowEventArgs ea = e as GridViewRowEventArgs;
//判断如果是底部,则执行
if (ea.Row.RowType == DataControlRowType.Footer)
{
DataRowView drv = ea.Row.DataItem as DataRowView;
// SQL 查询语句根据实际情况添加,要有合计或求平均值等函数
string strQuery = "select 'Total' as column1,'' as column2,sum(col3) as sale_col3,sum(col4)as sum_col4 from order_v" +
" where OrderNo = '" + Request.QueryString["OrderNo"] + "'";
//设置数据库连接
SqlConnection cn = new SqlConnection();
cn.ConnectionString = ConfigurationManager.ConnectionStrings["MainConnectionString"].ConnectionString;
cn.Open();
SqlCommand cmdGetItem = new SqlCommand(strQuery, cn);
SqlDataReader rdr;
rdr = cmdGetItem.ExecuteReader();
while (rdr.Read())
{
for (int i = 0; i < rdr.FieldCount; i++)
{
//填充每列的值
ea.Row.Cells[i].Text = rdr[i].ToString();
}
}
rdr.Close();
cn.Close();
}
}
文章引用自:
方法一:使用SQL查询统计出合计值,在绑定GridView时让其结果赋于一个DataTable(全局变量),然后在RowDataBound事件中
if (e.Row.RowType == DataControlRowType.Footer)
...{
e.Row.Cells[0].Text = "合计";
e.Row.Cells[3].Text = dtSum.Rows[0][0].ToString();
e.Row.Cells[4].Text = dtSum.Rows[0][1].ToString();
e.Row.Cells[5].Text = dtSum.Rows[0][2].ToString();
e.Row.Cells[6].Text = dtSum.Rows[0][3].ToString();
e.Row.Cells[7].Text = dtSum.Rows[0][4].ToString();
e.Row.Cells[8].Text = dtSum.Rows[0][5].ToString();
e.Row.Cells[9].Text = dtSum.Rows[0][6].ToString();
e.Row.Cells[10].Text = dtSum.Rows[0][7].ToString();
e.Row.Cells[11].Text = dtSum.Rows[0][8].ToString();
}
其中dtSum是那个全局DataTable,在绑定GridView同时将SQL查询的结果赋给它;效果如下:
方法二、直接把对应列每一行的值相加(不做数据查询,在RowDataBound事件中运算)
int mysum1 = 0;
int mysum2 = 0;
protected void GridList_RowDataBound(object sender, GridViewRowEventArgs e)
...{
if (e.Row.RowType == DataControlRowType.DataRow )
...{
DataRowView myrows=(DataRowView)e.Row.DataItem;
mysum1 +=Convert .ToInt32 (myrows[2].ToString ());
mysum2 += Convert.ToInt32(myrows[3].ToString());
}
// 合计
if (e.Row.RowType == DataControlRowType.Footer)
...{
e.Row.Cells[0].Text = "合计";
e.Row.Cells[1].Text = mysum1.ToString();
e.Row.Cells[2].Text = mysum2.ToString();
}
}
方法三:
1. 在Web Form 的源文件中,修改GridView部分,添加 OnRowDataBound 响应函数 OnRowDataBound = "GridView1_RowDataBound"
2. 在代码页,添加该函数代码如下:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRowEventArgs ea = e as GridViewRowEventArgs;
//判断如果是底部,则执行
if (ea.Row.RowType == DataControlRowType.Footer)
{
DataRowView drv = ea.Row.DataItem as DataRowView;
// SQL 查询语句根据实际情况添加,要有合计或求平均值等函数
string strQuery = "select 'Total' as column1,'' as column2,sum(col3) as sale_col3,sum(col4)as sum_col4 from order_v" +
" where OrderNo = '" + Request.QueryString["OrderNo"] + "'";
//设置数据库连接
SqlConnection cn = new SqlConnection();
cn.ConnectionString = ConfigurationManager.ConnectionStrings["MainConnectionString"].ConnectionString;
cn.Open();
SqlCommand cmdGetItem = new SqlCommand(strQuery, cn);
SqlDataReader rdr;
rdr = cmdGetItem.ExecuteReader();
while (rdr.Read())
{
for (int i = 0; i < rdr.FieldCount; i++)
{
//填充每列的值
ea.Row.Cells[i].Text = rdr[i].ToString();
}
}
rdr.Close();
cn.Close();
}
}
文章引用自:
相关文章推荐
- Scott Mitchell 的ASP.NET 2.0数据操作教程之十:使用 GridView 和DetailView实现的主/从报表
- ASP.NET - GridView 下增加自动编号列
- ASP.NET的gridview的页脚添加合计字段
- asp.net中GridView编辑,更新,合计用法示例
- ASP.NET—002:GridView手动增加一行
- Scott Mitchell 的ASP.NET 2.0数据教程之十:使用GridView 和DetailView实现的主/从报表
- asp.net GridView 中增加记录的方法
- ASP.net中GridView中增加一行记录并默认显示为编辑状态
- [翻译] Scott Mitchell 的ASP.NET 2.0数据操作教程之十:使用 GridView 和DetailView实现的主/从报表
- asp.net gridview中增加单击单元格事件
- asp.net GridView增加删除功能
- ASP.NET—002:GridView手动增加一行
- asp.net 中利用GridView实现增加,修改,删除
- asp.net GridView 中增加记录的方法
- Asp.net 2.0 GridView增加一列checkBox,并全选
- asp.net增加合计行
- asp.net 扩展GridView 增加单选按钮列的代码
- 在ASP.NET 2.0中操作数据之十:使用 GridView和DetailView实现的主/从报表