关于datagridview删除行问题(要保存到数据库)

来源:百度知道 编辑:UC知道 时间:2024/06/12 23:02:41
conn.Open();
string sql = "delete from bas_自定义规格 where ID='" + this.dataGridView1.CurrentRow.Cells[0].Value + "'";
SqlCommand cmd = new SqlCommand(sql,conn);
cmd.ExecuteNonQuery();
conn.Close();

运行后说“在将 varchar 值 'AAAA ' 转换成数据类型 int 时失败。”为什么??
那个“AAAA”是同一个表的另个字段储存的数据~。ID就肯定是INT型的。我的数据表有三个字段的

id不是this.dataGridView1.CurrentRow.Cells[0].Value

0是AAAA 应该没有这种ID吧

--------------------------------------
你dataGridView1 id显示在第几列?
显示在第二列用1
where ID=" + this.dataGridView1.CurrentRow.Cells[1].Value;

1.ID是什么类型 ?如果是INT 就不用 加 ' '
还有 你写代码的时候 注意用 TRY {} CATCH{} 异常!

ID是int吧
this.dataGridView1.CurrentRow.Cells[0].Value 你在两边加了 ‘’单引号,数据当成varchar处理了。int不用加单引号,而且你最好把this.dataGridView1.CurrentRow.Cells[0].Value 装成int的

Convert.toint3(this.dataGridView1.CurrentRow.Cells[0].Value)

如果你删除的行中有int或datetime等数据类型的话,要将''去掉:
where ID=" + this.dataGridView1.CurrentRow.Cells[0].Value.ToString()+ "";