private void btnAddTrained_Click(object sender, RoutedEventArgs e) { var tr = this.DataContext as Training; SelectTraineesControl w = new SelectTraineesControl(tr); if (w.ShowDialog() == true) { List <Trained> TraineesToRemove = new List <Trained>(); foreach (var tt in tr.Trainees) { if ((w.dgTrained.ItemsSource as List <Trained>).Where(x => x.TrainedID == tt.TrainedID && x.TrainedType == tt.TrainedType).FirstOrDefault() == null) { TraineesToRemove.Add(tt); } } foreach (var tt in TraineesToRemove) { if (tt.ID.HasValue) { Trained.DeleteData(tt); } tr.Trainees.Remove(tt); } foreach (var tt in w.dgTrained.ItemsSource as List <Trained> ) { if (tr.Trainees.Where(x => x.TrainedID == tt.TrainedID && x.TrainedType == tt.TrainedType).FirstOrDefault() == null) { tr.Trainees.Add(tt); if (tr.ID.HasValue) { Trained.InsertData(tt); } } } dgTrainees.Items.Refresh(); } }
private void btnSave_Click(object sender, RoutedEventArgs e) { var x = this.DataContext as Training; if (x.IsValidate()) { if (x.ID == null) { if (Training.InsertData(x)) { foreach (var t in x.Trainees) { Trained.InsertData(t); } MyMessage.InsertMessage(); } } else { foreach (var t in x.Trainees) { if (t.ID.HasValue) { Trained.UpdateData(t); } else { Trained.InsertData(t); } } if (Training.UpdateData(x)) { MyMessage.UpdateMessage(); } } } }