private void _setObjectEntities(ref DM_NHA_TAI_TRO item) { item.NTT_TEN = txtTenToChuc.Text; item.NTT_DIACHI = txtDiaChi.Text; item.NTT_CANHAN_TOCHUC = lueCaNhanToChuc.Text; item.NTT_LOAI = lueLoaiNhaTaiTro.Text; }
private void _saveData() { if (_validateControl()) { WaitDialogForm _wait = new WaitDialogForm("Đang lưu dữ liệu ...", "Vui lòng đợi giây lát"); using (var _context = new QL_HOIVIEN_KTEntities()) { DM_NHA_TAI_TRO item; switch (_formStatus) { case EnumFormStatus.ADD: item = new DM_NHA_TAI_TRO(); _setObjectEntities(ref item); _context.DM_NHA_TAI_TRO.Add(item); foreach (DM_NHA_TAI_TRO_CHITIET person in listNguoiDungDau) { person.DM_NHA_TAI_TRO = item; _context.DM_NHA_TAI_TRO_CHITIET.Add(person); } foreach (DM_NHA_TAI_TRO_CHITIET person in listNguoiQuanLy) { person.DM_NHA_TAI_TRO = item; _context.DM_NHA_TAI_TRO_CHITIET.Add(person); } break; case EnumFormStatus.MODIFY: Int64 id = Convert.ToInt64(gvGrid.GetFocusedRowCellValue(colNTT_ID)); item = (from p in _context.DM_NHA_TAI_TRO where p.NTT_ID == id select p).FirstOrDefault(); if (item != null) { _setObjectEntities(ref item); } var entity = _context.DM_NHA_TAI_TRO.Find(id); _context.Entry(entity).CurrentValues.SetValues(item); gvGrid_NguoiDungDau.PostEditor(); gvGrid_NguoiDungDau.UpdateCurrentRow(); gvGrid_NguoiQL.PostEditor(); gvGrid_NguoiQL.UpdateCurrentRow(); //add row foreach (DM_NHA_TAI_TRO_CHITIET person in listNguoiDungDau) { if (person.NTT_CT_ID == 0) { person.DM_NHA_TAI_TRO = item; _context.DM_NHA_TAI_TRO_CHITIET.Add(person); } } foreach (DM_NHA_TAI_TRO_CHITIET person in listNguoiQuanLy) { if (person.NTT_CT_ID == 0) { person.DM_NHA_TAI_TRO = item; _context.DM_NHA_TAI_TRO_CHITIET.Add(person); } } context.SaveChanges(); break; case EnumFormStatus.DELETE: Int64 deleteId = Convert.ToInt64(gvGrid.GetFocusedRowCellValue(colNTT_ID)); var entitiesNhaTaiTro = (from p in _context.DM_NHA_TAI_TRO where p.NTT_ID == deleteId select p).FirstOrDefault(); if (entitiesNhaTaiTro != null) { _context.DM_NHA_TAI_TRO.Remove(entitiesNhaTaiTro); } var entities = (from p in _context.DM_NHA_TAI_TRO_CHITIET where p.NTT_ID == deleteId select p); foreach (var item_delete in entities) { _context.DM_NHA_TAI_TRO_CHITIET.Remove(item_delete); } break; default: break; } _context.SaveChanges(); } FormStatus = EnumFormStatus.VIEW; _wait.Close(); } }