I have the next code that is executed each time the ADD ITEM button is clicked and that creates a new row with the data entered in the textBox above in the DataGridView below.
// AGREGA UN PRODUCTO
private void btnAgregarItem_Click(object sender, EventArgs e)
{
Pagos.Rows.Add(txtID.Text, txtNombre.Text, txtPrecio.Text, txtCantidad.Text, etiquetaSubTotalNum.Text);
string mesa = this.numMesa;
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 (mesa, id_producto, nombre, precio, cantidad, subtotal) VALUES (@mesa, @id_producto, @nombre, @precio, @cantidad, @subtotal)";
SQLiteCommand cmd = new SQLiteCommand(consulta, conexion);
cmd.Parameters.Clear();
cmd.Parameters.Add(new SQLiteParameter("@mesa", mesa));
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();
}
The function load () I need to run when a table is opened, so every time you click on the button of a table I bring the data from that table from my table Products in my database and I Insert that data from the table into the columns of my dataGridView:
public void cargar()
{
string mesa = this.numMesa;
string consulta = "select * from Productos where mesa = @mesa";
SQLiteCommand cmd = new SQLiteCommand(consulta, conexion);
cmd.Parameters.Clear();
cmd.Parameters.Add(new SQLiteParameter("@mesa", mesa));
using (SQLiteDataReader read = cmd.ExecuteReader())
{
while (read.Read())
{
Pagos.Rows.Add(new object[] {
read.GetValue(0),
//read.GetValue(read.GetOrdinal("mesa")),
read.GetValue(read.GetOrdinal("id_producto")),
read.GetValue(read.GetOrdinal("nombre")),
read.GetValue(read.GetOrdinal("precio")),
read.GetValue(read.GetOrdinal("cantidad")),
read.GetValue(read.GetOrdinal("subtotal"))
});
}
}
}
My product table is composed of the following columns:
mesa, id_producto, nombre, precio, cantidad, subtotal
My dataGridView
has the columns:
id_productos, nombre, precio, cantidad, subtotal.