public void panelButtons_Click(object sender, EventArgs e) { try { decimal quantity = 1; item_id = ((Button)sender).Name; DataTable dt = blord.getitembyitem_id(Convert.ToInt32(item_id)); bool Found = false; if (dataGridView1.Rows.Count > 0) { //Check if the product Id exists with the same Price foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells["cal_item_name"].Value.ToString() == ((Button)sender).Text && row.Cells["cal_order_id"].Value == null) { if (Convert.ToString(row.Cells["cal_item_name"].Value) == ((Button)sender).Text) { row.Cells["cal_qty"].Value = Convert.ToString(1 + Convert.ToInt32(row.Cells["cal_qty"].Value ?? String.Empty)); decimal cost = Convert.ToDecimal(row.Cells["cal_cost"].Value); decimal quantitys = Convert.ToInt32(row.Cells["cal_qty"].Value ?? String.Empty); decimal total = quantitys * cost; row.Cells["cal_total"].Value = total; Found = true; } } } if (!Found) { DataTable dt_check_modifier = blmd.get_all_modifier_by_item_id(Convert.ToInt32(item_id)); if (dt_check_modifier.Rows.Count > 0) { Item_modifier_list iml = new Item_modifier_list(); iml.item_id = item_id; iml.ShowDialog(); modifier_text = Item_modifier_list.md_text; int isi = dataGridView1.Rows.Add(); dataGridView1.Rows[isi].Cells["cal_item_name"].Value = dt.Rows[0]["item_name"].ToString(); dataGridView1.Rows[isi].Cells["cal_qty"].Value = quantity; dataGridView1.Rows[isi].Cells["cal_cost"].Value = dt.Rows[0]["item_price"].ToString(); decimal total = quantity * Convert.ToDecimal(dt.Rows[0]["item_price"].ToString()); dataGridView1.Rows[isi].Cells["cal_category_name"].Value = category_name; dataGridView1.Rows[isi].Cells["cal_total"].Value = total; dataGridView1.Rows[isi].Cells["calkot_print"].Value = dt.Rows[0]["kot_print"].ToString(); dataGridView1.Rows[isi].Cells["cal_description"].Value = modifier_text; dataGridView1.Rows[isi].Cells["cal_note"].Value = ""; dataGridView1.Rows[isi].DefaultCellStyle.BackColor = Color.DarkGray; } else { //Add the row to grid view int isi = dataGridView1.Rows.Add(); dataGridView1.Rows[isi].Cells["cal_item_name"].Value = dt.Rows[0]["item_name"].ToString(); dataGridView1.Rows[isi].Cells["cal_qty"].Value = quantity; dataGridView1.Rows[isi].Cells["cal_cost"].Value = dt.Rows[0]["item_price"].ToString(); decimal total = quantity * Convert.ToDecimal(dt.Rows[0]["item_price"].ToString()); dataGridView1.Rows[isi].Cells["cal_total"].Value = total; dataGridView1.Rows[isi].Cells["cal_category_name"].Value = category_name; dataGridView1.Rows[isi].Cells["calkot_print"].Value = dt.Rows[0]["kot_print"].ToString(); dataGridView1.Rows[isi].DefaultCellStyle.BackColor = Color.DarkGray; dataGridView1.Rows[isi].Cells["cal_note"].Value = ""; } } } else { DataTable dt_check_modifier = blmd.get_all_modifier_by_item_id(Convert.ToInt32(item_id)); if (dt_check_modifier.Rows.Count > 0) { Item_modifier_list iml = new Item_modifier_list(); iml.item_id = item_id; iml.ShowDialog(); modifier_text = Item_modifier_list.md_text; int isi = dataGridView1.Rows.Add(); dataGridView1.Rows[isi].Cells["cal_item_name"].Value = dt.Rows[0]["item_name"].ToString(); dataGridView1.Rows[isi].Cells["cal_qty"].Value = quantity; dataGridView1.Rows[isi].Cells["cal_cost"].Value = dt.Rows[0]["item_price"].ToString(); decimal total = quantity * Convert.ToDecimal(dt.Rows[0]["item_price"].ToString()); dataGridView1.Rows[isi].Cells["cal_category_name"].Value = category_name; dataGridView1.Rows[isi].Cells["cal_total"].Value = total; dataGridView1.Rows[isi].Cells["calkot_print"].Value = dt.Rows[0]["kot_print"].ToString(); dataGridView1.Rows[isi].Cells["cal_description"].Value = modifier_text; dataGridView1.Rows[isi].Cells["cal_note"].Value = ""; dataGridView1.Rows[isi].DefaultCellStyle.BackColor = Color.DarkGray; } else { //Add the row to grid view for the first time int isi = dataGridView1.Rows.Add(); dataGridView1.Rows[isi].Cells["cal_item_name"].Value = dt.Rows[0]["item_name"].ToString(); dataGridView1.Rows[isi].Cells["cal_qty"].Value = quantity; dataGridView1.Rows[isi].Cells["cal_cost"].Value = dt.Rows[0]["item_price"].ToString(); decimal total = quantity * Convert.ToDecimal(dt.Rows[0]["item_price"].ToString()); dataGridView1.Rows[isi].Cells["cal_category_name"].Value = category_name; dataGridView1.Rows[isi].Cells["cal_total"].Value = total; dataGridView1.Rows[isi].Cells["calkot_print"].Value = dt.Rows[0]["kot_print"].ToString(); dataGridView1.Rows[isi].DefaultCellStyle.BackColor = Color.DarkGray; dataGridView1.Rows[isi].Cells["cal_note"].Value = ""; } } calculate_total(); button_addwasclick = true; } catch (Exception ex) { MessageBox.Show(ex.Message); } }