GridView加入自动求和求平均值小计
效果图:
解决方案:
private double sum =
0;//取指定列的数据和,你要根据具体情况对待可能你要处理的是int
protected void
GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToDouble(e.Row.Cells[6].Text);
}
else if
(e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[5].Text = "总薪水为:";
e.Row.Cells[6].Text =
sum.ToString();
e.Row.Cells[3].Text = "平均薪水为:";
e.Row.Cells[4].Text = ((int)(sum /
GridView1.Rows.Count)).ToString();
}
}
后台全部代码:
using System;
using System.Data;
using
System.Configuration;
using System.Web;
using
System.Web.Security;
using System.Web.UI;
using
System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using
System.Drawing;
public partial class Default7 : System.Web.UI.Page
{
SqlConnection
sqlcon;
SqlCommand sqlcom;
string strCon = "Data
Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
protected void
Page_Load(object sender, EventArgs e)
{
if
(!IsPostBack)
{
bind();
}
}
protected void GridView1_RowEditing(object sender,
GridViewEditEventArgs e)
{
GridView1.EditIndex =
e.NewEditIndex;
bind();
}
protected void
GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string sqlstr =
"update 飞狐工作室 set 姓名='"
+
((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim()
+ "',家庭住址='"
+
((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim()
+ "' where 身份证号码='"
+
GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcom = new
SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}
protected void
GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
public
void bind()
{
string sqlstr = "select top 5 * from
飞狐工作室";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter
myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new
DataSet();
sqlcon.Open();
myda.Fill(myds,
"飞狐工作室");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "身份证号码" };
GridView1.DataBind();
sqlcon.Close();
}
private double
sum = 0;//取指定列的数据和
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowIndex >=
0)
{
sum +=
Convert.ToDouble(e.Row.Cells[6].Text);
}
else if
(e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[5].Text = "总薪水为:";
e.Row.Cells[6].Text =
sum.ToString();
e.Row.Cells[3].Text = "平均薪水为:";
e.Row.Cells[4].Text = ((int)(sum /
GridView1.Rows.Count)).ToString();
}
}
}
前台:唯一的花头就是设置ShowFooter="True"
,否则默认表头为隐藏的!
<asp:GridView ID="GridView1"
runat="server" AutoGenerateColumns="False" CellPadding="3"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating"
OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px"
OnRowDataBound="GridView1_RowDataBound" ShowFooter="True"
>
<FooterStyle BackColor="White"
ForeColor="#000066" />
<Columns>
<asp:CommandField
HeaderText="编辑" ShowEditButton="True" />
<asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True"
/>
<asp:BoundField DataField="姓名"
HeaderText="姓名" />
<asp:BoundField
DataField="出生日期" HeaderText="邮政编码" />
<asp:BoundField DataField="家庭住址" HeaderText="家庭住址"
/>
<asp:BoundField DataField="邮政编码"
HeaderText="邮政编码" />
<asp:BoundField
DataField="起薪" HeaderText="起薪" />
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White"
/>
<PagerStyle BackColor="White"
ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel
DataGridFixedHeader"/>
<HeaderStyle
BackColor="#006699" Font-Bold="True" ForeColor="White"
/>
</asp:GridView>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!