private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e) { string colName = dataGridView1.Columns[e.ColumnIndex].Name; if (colName == "Edit") { Frm2EditInvoices edit = new Frm2EditInvoices(); edit.lblID.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString(); edit.lblInvoiceNo.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString(); edit.txtCustomer.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString(); edit.txtAddress.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString(); edit.cbxPayment.Text = dataGridView1.Rows[e.RowIndex].Cells[11].Value.ToString(); edit.Dmdays.Text = dataGridView1.Rows[e.RowIndex].Cells[12].Value.ToString(); edit.txtBilling.Text = dataGridView1.Rows[e.RowIndex].Cells[13].Value.ToString(); edit.lblSalesTotal.Text = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString(); edit.lblDiscount.Text = dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString(); edit.lblVAT.Text = dataGridView1.Rows[e.RowIndex].Cells[8].Value.ToString(); edit.lblVatable.Text = dataGridView1.Rows[e.RowIndex].Cells[9].Value.ToString(); edit.lblTotalAmount.Text = dataGridView1.Rows[e.RowIndex].Cells[10].Value.ToString(); edit.dataGridView1.Rows.Clear(); int i = 0; cn.Open(); cm = new SqlCommand("SELECT * FROM tblInvoiceOrder WHERE invoiceno like '" + dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString() + "'", cn); dr = cm.ExecuteReader(); while (dr.Read()) { i++; edit.dataGridView1.Rows.Add(i, dr[0].ToString(), dr[7].ToString(), dr[4].ToString(), dr[5].ToString(), dr[6].ToString(), dr[8].ToString(), dr[9].ToString()); } cn.Close(); //Compute automatically the amount of the ordered products Frm2EditInvoices invoice = new Frm2EditInvoices(); invoice.GetOrderTotal(); edit.Show(); } else if (colName == "Delete") { if (MessageBox.Show("Are you sure you want to Delete this Invoice?", "Deleting Invoice", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { cn.Open(); cm = new SqlCommand("DELETE FROM tblInvoiceOrder WHERE invoiceno like '" + dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString() + "'", cn); cm.ExecuteNonQuery(); cn.Close(); LoadInvoiceRecords(); cn.Open(); cm = new SqlCommand("DELETE FROM tblInvoiceRecords WHERE invoiceno like '" + dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString() + "'", cn); cm.ExecuteNonQuery(); cn.Close(); MessageBox.Show("Invoice Successfully Deleted.", "Deleting Invoice", MessageBoxButtons.OK, MessageBoxIcon.Information); LoadInvoiceRecords(); } } }
private void txtQty_KeyPress(object sender, KeyPressEventArgs e) { try { if (e.KeyChar == 46) { //accept .(for cents or decimal places) } else if (e.KeyChar == 8) { //accept backspace } else if ((e.KeyChar < 48) || (e.KeyChar > 57)) //ascii code 48 - 57 between 0-9 { e.Handled = true; } //Enter = 13 if ((e.KeyChar == 13) && (txtQty.Text != String.Empty)) { bool found = false; String id = ""; cn.Open(); cm = new SqlCommand("SELECT * FROM tblInvoiceOrder WHERE invoiceno = @invoiceno and prodcode = @prodcode", cn); cm.Parameters.AddWithValue("@invoiceno", edit.lblInvoiceNo.Text); cm.Parameters.AddWithValue("@prodcode", prodcode); dr = cm.ExecuteReader(); dr.Read(); if (dr.HasRows) { found = true; id = dr["id"].ToString(); } else { // if found = false, it will crate new row for new product found = false; } dr.Close(); cn.Close(); if (found == true) { Frm2EditInvoices up = new Frm2EditInvoices(); for (int i = 0; i < up.dataGridView1.Rows.Count; i++) { up.dataGridView1.Rows[i].Cells[2].Value = txtQty.Text; } cn.Open(); cm = new SqlCommand("UPDATE tblInvoiceOrder SET qty = (qty + " + int.Parse(txtQty.Text) + ") WHERE id = '" + id + "'", cn); cm.ExecuteNonQuery(); cn.Close(); edit.LoadInvoiceOrder(); this.Dispose(); } else { FrmMain2 Main2 = new FrmMain2(); cn.Open(); cm = new SqlCommand("INSERT INTO tblInvoiceOrder (invoiceno, customer, address, prodcode, proddescrip, prodprice, qty, stockdate, name, payment, days, billaddress) VALUES (@invoiceno, @customer, @address, @code, @desc, @price, @qty, @date, @name, @payment, @days, @bill)", cn); cm.Parameters.AddWithValue("@invoiceno", transno); cm.Parameters.AddWithValue("@customer", edit.txtCustomer.Text); cm.Parameters.AddWithValue("@address", edit.txtAddress.Text); cm.Parameters.AddWithValue("@code", prodcode); cm.Parameters.AddWithValue("@desc", proddescrip); cm.Parameters.AddWithValue("@price", prodprice); cm.Parameters.AddWithValue("@qty", int.Parse(txtQty.Text)); cm.Parameters.AddWithValue("@name", Main2.lblUsername.Text); cm.Parameters.AddWithValue("@date", edit.bunifuDatepicker1.Value.ToString("ddMMMyyyy")); cm.Parameters.AddWithValue("@payment", edit.cbxPayment.Text);; cm.Parameters.AddWithValue("@days", edit.Dmdays.Text); cm.Parameters.AddWithValue("@bill", edit.txtBilling.Text); cm.ExecuteNonQuery(); cn.Close(); edit.LoadInvoiceOrder(); this.Dispose(); } } } catch (Exception ex) { cn.Close(); MessageBox.Show(ex.Message, "Invoice Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }