gridview列的汇总
Gridview 中我们会用到汇总的功能,因为前一段时间用到了,所以在此总结一下,作为以后的参考;
需要用到Gridview的RowDataBound事件, RowDataBound是在数据源与gridview的行绑定完成之后才执行RowDataBound方法,也就是说数据源每和gridview的行绑定完成之后都会触发此事件,首先是要把gridview的showfooter属性设置为true,以便我们利用footer行进行统计
- //统计
- Decimal mysum3 = 0;
- Decimal mysum4 = 0;
- Decimal mysum5 = 0;
- Decimal mysum6 = 0;
- Decimal mysum7 = 0;
- protected void gvResult_RowDataBound(object sender, GridViewRowEventArgs e)
- {}
因为数据是会带有小数点的,所以定义decimal类型的 全局变量,存放进行数据统计之和,然后触发RowDataBound事件
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- //统计
- if (!string.IsNullOrEmpty(e.Row.Cells[6].Text.ToString()) && e.Row.Cells[6].Text.ToString() != " ")
- {
- Decimal sum1 = decimal.Parse(e.Row.Cells[6].Text.ToString());
- mysum3mysum3 = mysum3+sum1;
- }
- if (!string.IsNullOrEmpty(e.Row.Cells[7].Text.ToString()) && e.Row.Cells[7].Text.ToString() != " ")
- {
- Decimal sum2 = decimal.Parse(e.Row.Cells[7].Text.ToString());
- mysum4mysum4 = mysum4 + sum2;
- }
- if (!string.IsNullOrEmpty(e.Row.Cells[8].Text.ToString()) && e.Row.Cells[8].Text.ToString() != " ")
- {
- Decimal sum3 = decimal.Parse(e.Row.Cells[8].Text.ToString());
- mysum5mysum5 = mysum5 + sum3;
- }
- if (!string.IsNullOrEmpty(e.Row.Cells[9].Text.ToString()) && e.Row.Cells[9].Text!=" ")
- {
- Decimal sum4 = decimal.Parse(e.Row.Cells[9].Text.ToString());
- mysum6mysum6 = mysum6 + sum4;
- }
- if (!string.IsNullOrEmpty(e.Row.Cells[10].Text.ToString()) && e.Row.Cells[10].Text.ToString() != " ")
- {
- Decimal sum5 = decimal.Parse(e.Row.Cells[10].Text.ToString());
- mysum7mysum7 = mysum7 + sum5;
- }
- }
当源数据行和gridview行绑定完成之后,需要判断行类型是不是数据行,如果是数据行就会进入以上代码,然后需要判断gridview行的列是不是为空,和不等于 然后把当前行的列值和之前已绑定的数据行的列值进行相加;
- if (e.Row.RowType == DataControlRowType.Footer)
- {
- e.Row.Cells[0].Text = "<span align='center' style='color:red'>合计:</span>";
- e.Row.Cells[6].Text ="<span align='center' style='color:red'>"+ mysum3.ToString()+"</span>";
- e.Row.Cells[7].Text = "<span align='center' style='color:red'>" + mysum4.ToString() + "</span>";
- e.Row.Cells[8].Text = "<span align='center' style='color:red'>" + mysum5.ToString() + "</span>";
- e.Row.Cells[9].Text = "<span align='center' style='color:red'>" + mysum6.ToString() + "</span>";
- e.Row.Cells[10].Text = "<span align='center' style='color:red'>" + mysum7.ToString() + "</span>";
- }
这里也是对绑定行进行判断,如果行类型是脚注行的话,进行脚注行的赋值操作,即把数据行的列之和赋值给脚注行的某列,赋值的同时可以给脚注列添加一些样式,把字体改为红色,更醒目
效果如下:
完整代码如下:
Decimal mysum3 = 0;
Decimal mysum4 = 0;
Decimal mysum5 = 0;
Decimal mysum6 = 0;
Decimal mysum7 = 0;
protected void gvResult_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//统计
if (!string.IsNullOrEmpty(e.Row.Cells[6].Text.ToString()) && e.Row.Cells[6].Text.ToString() != " ")
{
Decimal sum1 = decimal.Parse(e.Row.Cells[6].Text.ToString());
mysum3 = mysum3+sum1;
}
if (!string.IsNullOrEmpty(e.Row.Cells[7].Text.ToString()) && e.Row.Cells[7].Text.ToString() != " ")
{
Decimal sum2 = decimal.Parse(e.Row.Cells[7].Text.ToString());
mysum4 = mysum4 + sum2;
}
if (!string.IsNullOrEmpty(e.Row.Cells[8].Text.ToString()) && e.Row.Cells[8].Text.ToString() != " ")
{
Decimal sum3 = decimal.Parse(e.Row.Cells[8].Text.ToString());
mysum5 = mysum5 + sum3;
}
if (!string.IsNullOrEmpty(e.Row.Cells[9].Text.ToString()) && e.Row.Cells[9].Text!=" ")
{
Decimal sum4 = decimal.Parse(e.Row.Cells[9].Text.ToString());
mysum6 = mysum6 + sum4;
}
if (!string.IsNullOrEmpty(e.Row.Cells[10].Text.ToString()) && e.Row.Cells[10].Text.ToString() != " ")
{
Decimal sum5 = decimal.Parse(e.Row.Cells[10].Text.ToString());
mysum7 = mysum7 + sum5;
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "<span align='center' style='color:red'>合计:</span>";
e.Row.Cells[6].Text ="<span align='center' style='color:red'>"+ mysum3.ToString()+"</span>";
e.Row.Cells[7].Text = "<span align='center' style='color:red'>" + mysum4.ToString() + "</span>";
e.Row.Cells[8].Text = "<span align='center' style='color:red'>" + mysum5.ToString() + "</span>";
e.Row.Cells[9].Text = "<span align='center' style='color:red'>" + mysum6.ToString() + "</span>";
e.Row.Cells[10].Text = "<span align='center' style='color:red'>" + mysum7.ToString() + "</span>";
}
}
Decimal mysum4 = 0;
Decimal mysum5 = 0;
Decimal mysum6 = 0;
Decimal mysum7 = 0;
protected void gvResult_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//统计
if (!string.IsNullOrEmpty(e.Row.Cells[6].Text.ToString()) && e.Row.Cells[6].Text.ToString() != " ")
{
Decimal sum1 = decimal.Parse(e.Row.Cells[6].Text.ToString());
mysum3 = mysum3+sum1;
}
if (!string.IsNullOrEmpty(e.Row.Cells[7].Text.ToString()) && e.Row.Cells[7].Text.ToString() != " ")
{
Decimal sum2 = decimal.Parse(e.Row.Cells[7].Text.ToString());
mysum4 = mysum4 + sum2;
}
if (!string.IsNullOrEmpty(e.Row.Cells[8].Text.ToString()) && e.Row.Cells[8].Text.ToString() != " ")
{
Decimal sum3 = decimal.Parse(e.Row.Cells[8].Text.ToString());
mysum5 = mysum5 + sum3;
}
if (!string.IsNullOrEmpty(e.Row.Cells[9].Text.ToString()) && e.Row.Cells[9].Text!=" ")
{
Decimal sum4 = decimal.Parse(e.Row.Cells[9].Text.ToString());
mysum6 = mysum6 + sum4;
}
if (!string.IsNullOrEmpty(e.Row.Cells[10].Text.ToString()) && e.Row.Cells[10].Text.ToString() != " ")
{
Decimal sum5 = decimal.Parse(e.Row.Cells[10].Text.ToString());
mysum7 = mysum7 + sum5;
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "<span align='center' style='color:red'>合计:</span>";
e.Row.Cells[6].Text ="<span align='center' style='color:red'>"+ mysum3.ToString()+"</span>";
e.Row.Cells[7].Text = "<span align='center' style='color:red'>" + mysum4.ToString() + "</span>";
e.Row.Cells[8].Text = "<span align='center' style='color:red'>" + mysum5.ToString() + "</span>";
e.Row.Cells[9].Text = "<span align='center' style='color:red'>" + mysum6.ToString() + "</span>";
e.Row.Cells[10].Text = "<span align='center' style='color:red'>" + mysum7.ToString() + "</span>";
}
}