我用GRIDVIEW绑定SQL SERVER中的SCORE表,表中有学号(No),姓名(Sname),语文(CNE),数学(MAS),英语(ENG),总分(Total)几个字段,学号是主键,本打算点击编辑后,只要修改语文,数学,英语这三个字段的数值,点击更新后总分可以根据修改的数值做相应的update,
代码我是这样写的:
string No = GridView1.DataKeys[e.RowIndex].Value.ToString();
string Sname= ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string CN = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string MA= ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string EN= ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
string sqlStr = "Update SCORE Set Total='"+CN+"'+'"+MA+"'+'"+EN+"',CNE='"+CN+"',MAS='"+MA+"' ,ENG='"+EN+"' ,Sname='"+Sname+"'where No='"+No+"'";
比如有这样一行:
NO Sname CNE MAS ENG TOTAL
001 AA 90 80 80 250现在将英语(ENG)改为90,点击更新后出现的是:
001 AA 90 80 90 908090
========================================
请各位帮我看一下,这段代码应该怎么改,多谢了!
代码我是这样写的:
string No = GridView1.DataKeys[e.RowIndex].Value.ToString();
string Sname= ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string CN = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string MA= ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string EN= ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
string sqlStr = "Update SCORE Set Total='"+CN+"'+'"+MA+"'+'"+EN+"',CNE='"+CN+"',MAS='"+MA+"' ,ENG='"+EN+"' ,Sname='"+Sname+"'where No='"+No+"'";
比如有这样一行:
NO Sname CNE MAS ENG TOTAL
001 AA 90 80 80 250现在将英语(ENG)改为90,点击更新后出现的是:
001 AA 90 80 90 908090
========================================
请各位帮我看一下,这段代码应该怎么改,多谢了!
double total = double.Parse(CN)+double.Parse(MA)+double.Parse(EN);
string sqlStr = "Update SCORE Set Total="+total+",CNE='"+CN+"',MAS='"+MA+"' ,ENG='"+EN+"' ,Sname='"+Sname+"'where No='"+No+"'";
这样行不行?
int a= Convert.ToInt32( ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim());
int b = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim());
写了之后用断点 查看totle的指有没有累加
好像是因为程序执行的是UpdateCommand,而不是我在RowUpdating里写的代码。
能教教我怎么改吗?