So I'm creating a project, which is a Point of Sale, like those in fast food chains.
The buttons on my POS is created dynamically, depends on the values from my database, and now I'm having a hard time to compute the subtotal when I change the quantity of each item. I used DataGrid to list all the products ordered by the customer.
I created two buttons which is add
and minus
that can set the quantity of the selected row in the datagridview, I'm not sure if I got it right but the code is also provided below which computes the price of the selected item multiplied to the quantity.
My problem is, how can I compute the subtotal price, and the total quantity of items in my datagridview everytime I add items in my datagrid or I add or subtract in the quantity of the item.? The subtotal should reflect immediately EVERYTIME I add an item, or add or subtract an item.
Provided is a sample image to understand better what I want to happen in my project.
- public void quantity_change(object sender, EventArgs e)
- {
- var row = dataGridView1.CurrentRow;
-
- if (row == null || row.Index < 0)
- return;
- var unit = (sender == add) ? 1 : -1;
-
- var quantity = Convert.ToInt32(row.Cells["Quantity"].Value) + unit;
-
- row.Cells["Quantity"].Value = quantity;
- var rate = Convert.ToDouble(row.Cells["SellingPrice"].Value);
- row.Cells["TotalPrice"].Value = quantity * rate;
- }
- private void frmPOS_Load(object sender, EventArgs e)
- {
-
- dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
-
- add.Click += quantity_change;
- minus.Click += quantity_change;
-
- cmd = new MySqlCommand("SELECT * FROM tblmenu", dbConn);
- MySqlDataReader rdr = cmd.ExecuteReader();
-
- while (rdr.Read())
- {
- Button btn = new Button();
- btn.Text = rdr["menuName"].ToString();
- btn.Name = rdr["menuID"].ToString();
- btn.Width = 126;
- btn.Height = 80;
-
- btn.Click += delegate
- {
- dataGridView1.ClearSelection();
-
- MySqlConnection cnn2 = new MySqlConnection(sqlConn.connString);
- cnn2.Open();
- cmd = new MySqlCommand("SELECT menuName, menuPrice FROM tblmenu WHERE menuID = @id", cnn2);
- cmd.Parameters.AddWithValue("@id", btn.Name);
- MySqlDataReader rdr2 = cmd.ExecuteReader();
-
- while (rdr2.Read())
- {
-
- dataGridView1.Rows.Add(rdr2.GetString("menuName").ToUpper(), 1, rdr2.GetDouble("menuPrice"));
- }
-
-
- foreach (DataGridViewRow row in dataGridView1.Rows)
- {
- value = row.Cells["SellingPrice"].Value.ToString();
- row.Cells["TotalPrice"].Value = value;
- }
- };
-
-
- if (rdr["menuAvailability"].ToString() == "yes")
- {
- if (rdr["menuCategory"].ToString() == "Sandwiches")
- {
- flpSandwiches.Controls.Add(btn);
- }
- else if (rdr["menuCategory"].ToString() == "Appetizers")
- {
- flpAppetizers.Controls.Add(btn);
- }
- }
- }
-
- rdr.Close();
- }