private void btnAdd_Click(object sender, RoutedEventArgs e) { try { MyDbContext context = new MyDbContext(); TrainController trainController = new TrainController(); Train train = new Train(txtTrainType.Text, txtNumberTrain.Text); if (trainController.AddTrain(train)) { MessageBox.Show("Поезд успешно добавлен."); TrainDataGrid.ClearValue(ItemsControl.ItemsSourceProperty); List <Train> trains = context.Trains.ToList(); TrainDataGrid.ItemsSource = trains; } else { MessageBox.Show("Этот поезд уже существует или введенные данные имеют неверный формат."); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btnUpdate_Click(object sender, RoutedEventArgs e) { try { context.SaveChanges(); TrainDataGrid.ClearValue(ItemsControl.ItemsSourceProperty); List <Train> trains = context.Trains.ToList(); TrainDataGrid.ItemsSource = trains; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btnDelete_Click(object sender, RoutedEventArgs e) { try { MyDbContext context = new MyDbContext(); TrainController trainController = new TrainController(); Train train = TrainDataGrid.SelectedItem as Train; if (TrainDataGrid.SelectedItem != null) { context.Trains.Attach(train); trainController.RemoveTrain(train); MessageBox.Show("Поезд успешно удален."); } TrainDataGrid.ClearValue(ItemsControl.ItemsSourceProperty); List <Train> trains = context.Trains.ToList(); TrainDataGrid.ItemsSource = trains; } catch (Exception ex) { MessageBox.Show(ex.Message); } }