c# - 单击按钮时,如何从 DataGridView 和 SQLite 数据库中只删除一行?
问题描述
我正在开发一个由两个表单 Form1 和 Form2 组成的餐厅应用程序。
Form2 有几个文本框、一个 DataGridView 和一个名为“添加产品”的按钮。
当单击“添加产品”按钮时,在上面的文本框中输入的数据被“移动”到 dataGridView 中,并出现一个新的动态列,每行都有一个“擦除”按钮。如果用户单击“擦除”按钮,则该行将从 dataGridView 中删除。
我正在尝试做的是从我的数据库中删除该行信息。使用我发布的代码,我可以删除该行,但我也删除了一些具有相同规格的其他行,我不想这样做。
这是类属性:
double precio = 0;
double cantidad = 0;
double subtotal = 0;
double total = 0;
// THIS CODE ADDS A NEW ROW TO THE DATAGRIDVIEW AND TO THE DATABASE
private void btnAgregarItem_Click(object sender, EventArgs e)
{
Pagos.Rows.Add(txtID.Text, txtNombre.Text, txtPrecio.Text, txtCantidad.Text, etiquetaSubTotalNum.Text);
int id_producto = Convert.ToInt32(txtID.Text.ToString());
string nombre = txtNombre.Text.ToString();
double precio = Convert.ToDouble(txtPrecio.Text.ToString());
double cantidad = Convert.ToDouble(txtCantidad.Text.ToString());
double subtotal = Convert.ToDouble(etiquetaSubTotalNum.Text.ToString());
String consulta = "INSERT INTO Productos (id_producto, nombre, precio, cantidad, subtotal) VALUES(@id_producto, @nombre, @precio, @cantidad, @subtotal)";
SQLiteCommand cmd = new SQLiteCommand(consulta, conexion);
cmd.Parameters.Clear();
cmd.Parameters.Add(new SQLiteParameter("@id_producto", id_producto));
cmd.Parameters.Add(new SQLiteParameter("@nombre", nombre));
cmd.Parameters.Add(new SQLiteParameter("@precio", precio));
cmd.Parameters.Add(new SQLiteParameter("@cantidad", cantidad));
cmd.Parameters.Add(new SQLiteParameter("@subtotal", subtotal));
cmd.ExecuteNonQuery();
}
// THIS CODE ERASES THE CURRENT ROW FROM THE DATAGRIDVIEW, BUT IT´S POSSIBLE THAT IT ALSO ERASES MORE THAN ONE LINE FROM THE DATABASE
private void Pagos_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (this.Pagos.Columns[e.ColumnIndex].Name == "colBotones")
{
Pagos.Rows.RemoveAt(Pagos.CurrentRow.Index);
int id_producto = Convert.ToInt32(txtID.Text.ToString());
string nombre = txtNombre.Text.ToString();
double precio = Convert.ToDouble(txtPrecio.Text.ToString());
double cantidad = Convert.ToDouble(txtCantidad.Text.ToString());
double subtotal = Convert.ToDouble(etiquetaSubTotalNum.Text.ToString());
String consulta = "DELETE FROM Productos where id_producto = @id_producto and nombre = @nombre and precio = @precio and cantidad = @cantidad and subtotal = @subtotal";
SQLiteCommand cmd = new SQLiteCommand(consulta, conexion);
cmd.Parameters.Clear();
cmd.Parameters.Add(new SQLiteParameter("@id_producto", id_producto));
cmd.Parameters.Add(new SQLiteParameter("@nombre", nombre));
cmd.Parameters.Add(new SQLiteParameter("@precio", precio));
cmd.Parameters.Add(new SQLiteParameter("@cantidad", cantidad));
cmd.Parameters.Add(new SQLiteParameter("@subtotal", subtotal));
cmd.ExecuteNonQuery();
}
}
解决方案
推荐阅读
- pyspark - TypeError: filter() 得到了一个意外的关键字参数
- java - 转换数字的递归函数产生反向输出
- sql - 在 SQLite 中检查数字约束
- c# - 使用 C# (Unity) 为 FPS 游戏添加后坐力
- mariadb - 如何在 MariaDB 5.5 中为单个派生表设置多个别名?
- java - Google Kickstart 编译器运行时错误,但代码在 intellij 中工作
- git - 正确使用带百分号的命令
- xcode - 如何在 xCode13 中禁用下拉建议
- mongodb - Kubernetes 持久卷小于请求
- javascript - Java Script/Vue.Js 根据名称按对象分组