private void btnLuu_Click(object sender, EventArgs e) { try { string mact = txtMaCT.Text; string mavp = txtMaVP.Text; string mahd = txtMaHD.Text; float tiencoc = float.Parse(txtTienCoc.Text); DateTime ngaylap = DateTime.Parse(dtpNgayLap.Value.ToString("dd/MM/yyyy")); if (Flag == true) { if (db.tbl_ChiTietHopDong.SqlQuery("select * from tbl_ChiTietHopDong").Where(m => m.MaChiTiet.Contains(txtMaCT.Text)).Count() > 0) { XtraMessageBox.Show("Mã chi tiết nhập sai hoặc bị trùng", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { tbl_ChiTietHopDong ct = new tbl_ChiTietHopDong(); ct.MaChiTiet = mact; ct.MaVanPhong = mavp; ct.MaHopDong = mahd; ct.TienDatCoc = tiencoc; ct.NgayLap = ngaylap; db.tbl_ChiTietHopDong.Add(ct); db.SaveChanges(); HienThiChiTietHD(); XtraMessageBox.Show("Thêm chi tiết thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information); setNull(); setButton(true); setKhoa(true); } } else { tbl_ChiTietHopDong ct = db.tbl_ChiTietHopDong.Where(m => m.MaChiTiet == mact).FirstOrDefault(); ct.MaVanPhong = mavp; ct.MaHopDong = mahd; ct.TienDatCoc = tiencoc; ct.NgayLap = ngaylap; if (ct == null) { XtraMessageBox.Show("Chi tiết hợp đồng không tồn tại!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { db.Entry(ct).State = EntityState.Modified; db.SaveChanges(); HienThiChiTietHD(); XtraMessageBox.Show("Cập nhật thành công!", "Thông Báo"); setNull(); setButton(true); setKhoa(true); } } } catch { } }
private void btnXoa_Click(object sender, EventArgs e) { if (dgvChiTietHD.SelectedRows.Count > 0) { DialogResult dr = XtraMessageBox.Show("Bạn có muốn xóa không ?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (dr == DialogResult.Yes) { string mact = txtMaCT.Text; tbl_ChiTietHopDong ct = db.tbl_ChiTietHopDong.Find(mact); db.tbl_ChiTietHopDong.Remove(ct); db.SaveChanges(); HienThiChiTietHD(); setNull(); } } else { XtraMessageBox.Show("Vui lòng chọn dòng cần xóa"); } }