C#中如何给GridView添加页脚以实现统计某一列值的和
第一步:设置GridView的属性:ShowFooter=true第二步:定义一个全局变量,以备使用,如:
在GridViewShow()函数中:protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){ if (e.Row.RowType == DataControlRowType.DataRow) { //----------鼠标移动到每项时颜色交替效果 e.Row.Attributes["onmouseover"] = "e=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'"; e.Row.Attributes["onmouseout"] = "this.style.backgroundColor=e"; //----------设置悬浮鼠标指针形状为小手" e.Row.Attributes["style"] = "Cursor:hand"; //----------鼠标点击某行即选中某行 e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex); } if (GridView1.FooterRow != null) { GridView1.FooterRow.Visible = true; } if (e.Row.RowType == DataControlRowType.Footer) // 判断当前项是否为页脚 { e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right; //因为示例中该GridView为7列 e.Row.Cells[4].Text = "费用合计:"; e.Row.Cells[5].Text = thisTotal.ToString()+" 元"; e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Center; e.Row.Cells[6].Text = "共计:"+total+" 元"; e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Center; }}
这样,便可以实现统计某一列的值了。