private void btnSave_Click(object sender, EventArgs e) { // บันทึกข้อมูลลงตาราง tbSale -> ฟิลด์ที่จะบันทึก SaleID , EmpID, SaleDate, TotalAmount VRUBookShop db = new VRUBookShop(); tbSale tbsale = new tbSale(); tbsale.EmpID = int.Parse(labEmpID.Text); tbsale.SaleDate = DateTime.Now; tbsale.TotalAmount = int.Parse(labTotalAmount.Text); db.tbSale.Add(tbsale); db.SaveChanges(); // นำข้อมูลการขายใน DataGridView บันทึกลงตาราง tbSaleDeatail -> ID, SaleID, BookID, Cost, Price, Amount, Total // อ่านค่า SaleID ออกมาก่อน int EmpID = int.Parse(labEmpID.Text); var row = db.tbSale.Where(sa => sa.EmpID == EmpID).OrderByDescending(sa => sa.SaleID).ToList(); int SaleID = row[0].SaleID; // วนรอบอ่านข้อมูลใน DataGridView เพื่อจัดเก็บลงตาราง tbSaleDetail for (int i = 0; i < dgvSale.Rows.Count - 1; i++) { tbSaleDetail tbsaledetail = new tbSaleDetail(); tbsaledetail.SaleID = SaleID; tbsaledetail.BookID = int.Parse(dgvSale.Rows[i].Cells["BookID"].Value.ToString()); tbsaledetail.Cost = int.Parse(dgvSale.Rows[i].Cells["Cost"].Value.ToString()); tbsaledetail.Price = int.Parse(dgvSale.Rows[i].Cells["Price"].Value.ToString()); tbsaledetail.Amount = int.Parse(dgvSale.Rows[i].Cells["Amount"].Value.ToString()); tbsaledetail.Total = int.Parse(dgvSale.Rows[i].Cells["Total"].Value.ToString()); db.tbSaleDetail.Add(tbsaledetail); db.SaveChanges(); // ตัดสต็อก update จำนวน ในตาราง tbBook int BookID = int.Parse(dgvSale.Rows[i].Cells["BookID"].Value.ToString()); tbBooks tbbook = new tbBooks(); var book = db.tbBooks.Single(b => b.BookID == BookID); book.Stock = book.Stock - int.Parse(dgvSale.Rows[i].Cells["Amount"].Value.ToString()); db.SaveChanges(); } this.clearDgvSale(); i = 0; MessageBox.Show("บันทึกการขายแล้ว", "ผลลัพธ์", MessageBoxButtons.OK, MessageBoxIcon.Information); labTotalAmount.Text = ""; this.totalAll = 0; }
private void tsbEdit_Click(object sender, EventArgs e) { if (labBookTypeID.Text == "") { this.showErrorMessage("เลือกข้อมูลที่จะแก้ไขก่อน"); return; } int BookTypeID = int.Parse(labBookTypeID.Text); VRUBookShop db = new VRUBookShop(); var bt = db.tbBookType.Single(bts => bts.BookTypeID.Equals(BookTypeID)); bt.BookTypeName = txtBookTypeName.Text; db.SaveChanges(); this.showDgvBookType(); this.clear(); }
private void tsbSave_Click(object sender, EventArgs e) { if (txtBookTypeName.Text == "") { this.showErrorMessage("ป้อนประเภทหนังสือก่อน"); return; } VRUBookShop db = new VRUBookShop(); tbBookType tb = new tbBookType(); tb.BookTypeName = txtBookTypeName.Text; db.tbBookType.Add(tb); db.SaveChanges(); this.showDgvBookType(); this.clear(); // int bookTypeID = db.tbBookType.Max(bt => bt.BookTypeID); // MessageBox.Show(bookTypeID.ToString()); var bookType = db.tbBookType.Single(bt => bt.BookTypeID == 12); MessageBox.Show(bookType.BookTypeID.ToString()); }
private void tsbDelete_Click(object sender, EventArgs e) { if (labBookTypeID.Text == "") { this.showErrorMessage("เลือกข้อมูลที่จะลบก่อน"); return; } DialogResult result; result = MessageBox.Show("ต้องการลบข้อมูลใช่หรือไม่", "ยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.No) { return; } int BookTypeID = int.Parse(labBookTypeID.Text); VRUBookShop db = new VRUBookShop(); var bt = db.tbBookType.Single(bts => bts.BookTypeID.Equals(BookTypeID)); db.tbBookType.Remove(bt); db.SaveChanges(); this.showDgvBookType(); this.clear(); }