int id = 0;
try
{
id = (int)dgv_inventory.CurrentRow.Cells[0].Value;
}
catch (System.Exception ex)
{
MessageBox.Show("请选择有效数据行!");
return;
}
if (MessageBox.Show("确定要删除吗?", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
return;
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
"Server=(local);Database=DB_Equipment;Integrated Security=sspi";
using (SqlConnection conn = new SqlConnection(sqlConnection.ConnectionString))
{
string sql = string.Format("delete from tb_inventory where EquipmentNo={0}", id);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
int n = Convert.ToInt32(cmd.ExecuteNonQuery());
if (n != 1)
{
MessageBox.Show("删除失败!");
}
else
{
MessageBox.Show("删除成功!");
}
}
以上为删除按钮事件中的代码,运行时只能运行到try_catch语句,即一直跳出窗体提示“请选择有效数据行”,这是哪里出现了问题?

c#winform中datagridview删除数据库中的整行数据
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-