public void CancelEdit() { this.PersonId = this.backupData.personId; this.DateIn = this.backupData.datein; this.DateOut = this.backupData.dateout; this.Departments = this.backupData.departments; this.SelectedPerson = this.backupData.selectedPerson; //if (this.Departments != null) //{ // foreach (HSDepartment l in this.Departments) // { // DataSource.Emp_Dept lEmpDept = lDataContext.Emp_Depts.FirstOrDefault(p => p.EmployeeId == this.Id && p.DepartmentId == l.Id); // l.IsSelected = true; // if (lEmpDept == null) // l.IsSelected = false; // } // this.OnPropertyChanged(() => this.Departments); //} foreach (HSDepartment l in this.Departments ?? Enumerable.Empty <HSDepartment>()) { DataSource.Emp_Dept lEmpDept = lDataContext.Emp_Depts.FirstOrDefault(p => p.EmployeeId == this.Id && p.DepartmentId == l.Id); l.IsSelected = true; if (lEmpDept == null) { l.IsSelected = false; } } this.OnPropertyChanged(() => this.Departments); this.OnPropertyChanged(() => this.SelectedPerson); }
public void EndEdit() { Boolean lChanged = false; DataSource.Employee lUpdate = lDataContext.Employees.SingleOrDefault(p => p.Id == this.Id); if (lUpdate == null) { DataSource.Employee newItem = new DataSource.Employee { PersonId = this.PersonId, DateIn = this.DateIn, DateOut = this.DateOut, }; // Add the new object to the Orders collection. lDataContext.Employees.InsertOnSubmit(newItem); // Submit the change to the database. try { lDataContext.SubmitChanges(); this.Id = newItem.Id; } catch (Exception e) { Console.WriteLine(e); } } else { lUpdate.PersonId = this.PersonId; lUpdate.DateIn = this.DateIn; lUpdate.DateOut = this.DateOut; lDataContext.SubmitChanges(); } DataSource.Emp_Dept lEmpDept; foreach (HSDepartment l in this.Departments) { lEmpDept = lDataContext.Emp_Depts.SingleOrDefault(p => p.EmployeeId == this.Id && p.DepartmentId == l.Id); if (lEmpDept == null) { if (l.IsSelected) { DataSource.Emp_Dept newEmpDept = new DataSource.Emp_Dept { EmployeeId = this.Id, DepartmentId = l.Id }; // Add the new object to the Orders collection. lDataContext.Emp_Depts.InsertOnSubmit(newEmpDept); lChanged = true; } } else { if (l.IsSelected) { lEmpDept.EmployeeId = this.Id; lEmpDept.DepartmentId = l.Id; } else { lDataContext.Emp_Depts.DeleteOnSubmit(lEmpDept); } lChanged = true; } try { if (lChanged) { lDataContext.SubmitChanges(); } } catch (Exception e) { Console.WriteLine(e); } } }