private void btnAdd_Click(object sender, RoutedEventArgs e) { MyDbContext context = new MyDbContext(); TripController tripController = new TripController(); if (double.TryParse(txtCost.Text, out double cost) == true && int.TryParse(txtNumberSeats.Text, out int number) == true && int.TryParse(txtPlaceDepartment.Text, out int departure) == false && int.TryParse(txtPlaceArrival.Text, out int arrival) == false) { double costTrip = double.Parse(txtCost.Text); int numberSeats = int.Parse(txtNumberSeats.Text); Trip trip = new Trip(dateDeparture.SelectedDate, dateArrival.SelectedDate, txtPlaceDepartment.Text, txtPlaceArrival.Text, costTrip, int.Parse(txtNumberSeats.Text), txtNumberTrain.Text, txtType.Text); if (tripController.AddTrip(trip)) { MessageBox.Show("Рейс успешно добавлен."); TripDataGrid.ClearValue(ItemsControl.ItemsSourceProperty); List <Trip> trips = context.Trips.Include(tr => tr.Train).ToList(); TripDataGrid.ItemsSource = trips; } else { MessageBox.Show("Этот рейс уже существует или введенные данные имеют неверный формат."); } } else { MessageBox.Show("Неверный формат данных"); } }
private void btnUpdate_Click(object sender, RoutedEventArgs e) { try { context.SaveChanges(); TripDataGrid.ClearValue(ItemsControl.ItemsSourceProperty); List <Trip> trips = context.Trips.ToList(); TripDataGrid.ItemsSource = trips; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btnDelete_Click(object sender, RoutedEventArgs e) { try { MyDbContext context = new MyDbContext(); TripController tripController = new TripController(); Trip trip = TripDataGrid.SelectedItem as Trip; if (TripDataGrid.SelectedItem != null) { context.Trips.Attach(trip); tripController.RemoveTrip(trip); MessageBox.Show("Рейс успешно удален."); } TripDataGrid.ClearValue(ItemsControl.ItemsSourceProperty); List <Trip> trips = context.Trips.Include(tr => tr.Train).ToList(); TripDataGrid.ItemsSource = trips; } catch (Exception ex) { MessageBox.Show(ex.Message); } }