我在gridview的第3列中插入值1,2和3,在第4列中,我希望得到答案为6,但在第四列中,值正在追加,值类似于123在第四栏。下面我已粘贴代码
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
string ID = (row.Cells[1].Text);
string Assignment_Marks =(row.Cells[2].Controls[0] as TextBox).Text;
string Viva_Marks = (row.Cells[3].Controls[0] as TextBox).Text;
string Midterm_Marks = (row.Cells[4].Controls[0] as TextBox).Text;
string Overall_Marks = (row.Cells[5].Controls[0] as TextBox).Text;
string status = (row.Cells[6].Controls[0] as TextBox).Text;
string constr = ConfigurationManager.ConnectionStrings["StudentConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE users SET Assignment_Marks = @Assignment_Marks, Viva_Marks = @Viva_Marks, Midterm_Marks = @Midterm_Marks, Overall_Marks= @Overall_Marks, status=@status where div='A' and year='3' and stream='IT' and ID=@ID"))
{
cmd.Parameters.AddWithValue("@ID", ID);
cmd.Parameters.AddWithValue("@Assignment_Marks", Assignment_Marks);
cmd.Parameters.AddWithValue("@Viva_Marks", Viva_Marks);
cmd.Parameters.AddWithValue("@Midterm_Marks", Midterm_Marks);
Overall_Marks = Assignment_Marks + Viva_Marks + Midterm_Marks;//It is use to append i know, then what to use
cmd.Parameters.AddWithValue("@Overall_Marks", Overall_Marks);
cmd.Parameters.AddWithValue("@status", status);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
GridView1.EditIndex = -1;
this.BindGrid();
}
答案 0 :(得分:0)
因为所有变量都是字符串类型,当你加上两个字符串时,它会追加。 你必须将字符串转换为int类型,然后它将汇总所有数字。
int SumUp = Int32.Parse(Assignment_Marks) + Int32.Parse(Viva_Marks) + Int32.Parse(Midterm_Marks);
Overall_Marks = SumUp.ToString();
答案 1 :(得分:0)
上面提到的值以字符串格式存储。所以我们要转换十进制格式。因为标记可能是十进制格式.eg:70.5
请告诉我下面提到的代码是否有效。如果这个答案对你有用,请点击答案选项等。
decimal OM=0m; OM=Convert.ToDecimal(Assignment_Marks)+Convert.ToDecimal(Viva_Marks)+Convert.ToDecimal(Midterm_Marks);
Overall_Marks = OM.ToString();