Compute Subtotal of POS

May 21 2016 4:32 AM

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.

  1. public void quantity_change(object sender, EventArgs e)  
  2.     {  
  3.         var row = dataGridView1.CurrentRow;  
  4.   
  5.         if (row == null || row.Index < 0)  
  6.             return;  
  7.         var unit = (sender == add) ? 1 : -1;  
  8.   
  9.         var quantity = Convert.ToInt32(row.Cells["Quantity"].Value) + unit;  
  10.   
  11.         row.Cells["Quantity"].Value = quantity;  
  12.         var rate = Convert.ToDouble(row.Cells["SellingPrice"].Value);  
  13.         row.Cells["TotalPrice"].Value = quantity * rate;  
  14.     }  
  1. private void frmPOS_Load(object sender, EventArgs e)  
  2.     {  
  3.   
  4.         dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;  
  5.   
  6.         add.Click += quantity_change;  
  7.         minus.Click += quantity_change;  
  8.   
  9.         cmd = new MySqlCommand("SELECT * FROM tblmenu", dbConn);  
  10.         MySqlDataReader rdr = cmd.ExecuteReader();  
  11.   
  12.         while (rdr.Read())  
  13.         {  
  14.             Button btn = new Button();  
  15.             btn.Text = rdr["menuName"].ToString();  
  16.             btn.Name = rdr["menuID"].ToString();  
  17.             btn.Width = 126;  
  18.             btn.Height = 80;  
  19.   
  20.             btn.Click += delegate  
  21.             {  
  22.                 dataGridView1.ClearSelection();  
  23.   
  24.                 MySqlConnection cnn2 = new MySqlConnection(sqlConn.connString);  
  25.                 cnn2.Open();  
  26.                 cmd = new MySqlCommand("SELECT menuName, menuPrice FROM tblmenu WHERE menuID = @id", cnn2);  
  27.                 cmd.Parameters.AddWithValue("@id", btn.Name);  
  28.                 MySqlDataReader rdr2 = cmd.ExecuteReader();  
  29.   
  30.                 while (rdr2.Read())  
  31.                 {  
  32.                    //I added the item in my datagridview, with the button name, 1 = 1quantity, and Selling Price  
  33.                  dataGridView1.Rows.Add(rdr2.GetString("menuName").ToUpper(), 1, rdr2.GetDouble("menuPrice"));  
  34.                 }  
  35.   
  36.                 //I copied the value of Selling Price Column to the Total Price Column in this part  
  37.                 foreach (DataGridViewRow row in dataGridView1.Rows)  
  38.                 {  
  39.                     value = row.Cells["SellingPrice"].Value.ToString();  
  40.                     row.Cells["TotalPrice"].Value = value;  
  41.                 }  
  42.             };  
  43.   
  44.   
  45.             if (rdr["menuAvailability"].ToString() == "yes")  
  46.             {  
  47.                 if (rdr["menuCategory"].ToString() == "Sandwiches")  
  48.                 {  
  49.                     flpSandwiches.Controls.Add(btn);  
  50.                 }  
  51.                 else if (rdr["menuCategory"].ToString() == "Appetizers")  
  52.                 {  
  53.                     flpAppetizers.Controls.Add(btn);  
  54.                 }  
  55.             }                 
  56.         }  
  57.   
  58.         rdr.Close();  
  59.     }