private void btn_themvatdung_Click(object sender, EventArgs e) { if (txt_themvatdung.Text == "" || txt_themdongia.Text == "" || Convert.ToInt32(nUP_themsoluong.Value) == 0) { MessageBox.Show("Vui lòng nhập đầy đủ thông tin", "Cảnh báo"); } else { double dg; try { dg = Convert.ToDouble(txt_themdongia.Text); } catch (Exception s) { MessageBox.Show("Đơn giá không hợp lệ", "Cảnh báo"); return; } chitietcanho ctch = new chitietcanho { mavatdung = ChitietcanhoBLL.Instance.GetMaxMavatdung() + 1, macanho = Convert.ToInt32(dgV_canho.SelectedRows[0].Cells[0].Value), vatdung = txt_themvatdung.Text, soluong = Convert.ToInt32(nUP_themsoluong.Value), dongia = dg }; if (ChitietcanhoBLL.Instance.ThemVatdung(ctch) == false) { MessageBox.Show("Thêm thất bại", "Thông báo"); return; } Show_Vatdung(Convert.ToInt32(dgV_canho.SelectedRows[0].Cells[0].Value)); mydel(); } }
public bool SuaVatdung(chitietcanho ctch) { try { CSDL db = new CSDL(); chitietcanho l1 = db.chitietcanhoes.Find(ctch.mavatdung); l1.vatdung = ctch.vatdung; l1.soluong = ctch.soluong; l1.dongia = ctch.dongia; db.SaveChanges(); return(true); } catch (Exception e) { return(false); } }
public bool Xoavatdung(int mavatdung) { try { CSDL db = new CSDL(); chitietcanho l1 = db.chitietcanhoes.Find(mavatdung); if (l1.soluong > 1) { l1.soluong--; l1.dongia -= l1.dongia / 2; } else { db.chitietcanhoes.Remove(l1); } db.SaveChanges(); return(true); } catch (Exception e) { return(false); } }
public bool ThemVatdung(chitietcanho ctch) { try { CSDL db = new CSDL(); chitietcanho l1 = db.chitietcanhoes.Where(p => p.macanho == ctch.macanho && p.vatdung == ctch.vatdung).FirstOrDefault(); if (l1 == null) { db.chitietcanhoes.Add(ctch); } else { l1.soluong += ctch.soluong; l1.dongia += ctch.dongia; } db.SaveChanges(); return(true); } catch (Exception e) { return(false); } }
private void btn_suavatdung_Click(object sender, EventArgs e) { if (dgV_vatdung.SelectedRows.Count != 1) { MessageBox.Show("Vui lòng chỉ chọn 1 vật dụng", "Cảnh báo"); } else { if (txt_suavatdung.ReadOnly == true) { DialogResult t = MessageBox.Show("Bạn có muốn sửa?", "Thông báo", MessageBoxButtons.OKCancel); if (t == DialogResult.OK) { dgV_vatdung.Enabled = false; btn_themvatdung.Enabled = false; btn_xoavatdung.Enabled = false; btn_xoa.Enabled = false; btn_them.Enabled = false; dgV_canho.Enabled = false; txt_suavatdung.ReadOnly = false; txt_suadongia.ReadOnly = false; nUP_suasoluong.Enabled = true; txt_suavatdung.Focus(); } } else { if (txt_suadongia.Text == "" || txt_suavatdung.Text == "" || Convert.ToInt32(nUP_suasoluong.Value) == 0) { MessageBox.Show("Vui lòng nhập đầy đủ thông tin", "Cảnh báo"); } else { double dg; try { dg = Convert.ToDouble(txt_suadongia.Text); } catch (Exception s) { MessageBox.Show("Đơn giá không hợp lệ", "Cảnh báo"); return; } chitietcanho ctch = new chitietcanho { mavatdung = Convert.ToInt32(dgV_vatdung.SelectedRows[0].Cells[0].Value), macanho = Convert.ToInt32(dgV_vatdung.SelectedRows[0].Cells[1].Value), vatdung = txt_suavatdung.Text, soluong = Convert.ToInt32(nUP_suasoluong.Value), dongia = dg, }; if (ChitietcanhoBLL.Instance.SuaVatdung(ctch) == false) { MessageBox.Show("Sửa thất bại", "Thông báo"); return; } dgV_vatdung.Enabled = true; btn_themvatdung.Enabled = true; btn_xoavatdung.Enabled = true; btn_xoa.Enabled = true; btn_them.Enabled = true; dgV_canho.Enabled = true; txt_suavatdung.ReadOnly = true; txt_suadongia.ReadOnly = true; nUP_suasoluong.Enabled = false; Show_Vatdung(Convert.ToInt32(dgV_canho.SelectedRows[0].Cells[0].Value)); mydel(); } } } }