AddCar.xaml.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. namespace SSS
  15. {
  16. /// <summary>
  17. /// Логика взаимодействия для AddCar.xaml
  18. /// </summary>
  19. public partial class AddCar : Page
  20. {
  21. Entities DB = new Entities();
  22. bool flag = false;
  23. public AddCar()
  24. {
  25. InitializeComponent();
  26. Mark.ItemsSource = DB.CarBrand.ToList();
  27. Mark.DisplayMemberPath = "CarBrand1";
  28. Worker.ItemsSource = DB.Teem.ToList();
  29. Worker.DisplayMemberPath = "Surname_Teem";
  30. TypeService.ItemsSource = DB.Service.ToList();
  31. TypeService.DisplayMemberPath = "name_Service";
  32. Client.ItemsSource = DB.Client.ToList();
  33. Client.DisplayMemberPath = "Surname";
  34. }
  35. public AddCar(int id)
  36. {
  37. InitializeComponent();
  38. flag = true;
  39. Car OldCar = DB.Car.Find(id);
  40. Mark.ItemsSource = DB.CarBrand.ToList();
  41. Mark.DisplayMemberPath = "CarBrand1";
  42. Worker.ItemsSource = DB.Teem.ToList();
  43. Worker.DisplayMemberPath = "Surname_Teem";
  44. TypeService.ItemsSource = DB.Service.ToList();
  45. TypeService.DisplayMemberPath = "name_Service";
  46. Client.ItemsSource = DB.Client.ToList();
  47. Client.DisplayMemberPath = "Surname";
  48. CarBrand cr = DB.CarBrand.Where(x => x.id_CarBrand == OldCar.id_CarBrand).FirstOrDefault();
  49. Client client = DB.Client.Where(x => x.id_Client == OldCar.id_Client).FirstOrDefault();
  50. Mark.SelectedItem = cr;
  51. Worker.SelectedItem = DB.Teem.Where(x => x.id_Teem == OldCar.id_Teem).FirstOrDefault();
  52. TypeService.SelectedItem = DB.Service.Where(x => x.id_Service == OldCar.id_Service).FirstOrDefault();
  53. Client.SelectedItem = client;
  54. Mileage.Text = Convert.ToString(OldCar.Car_mileage);
  55. }
  56. private void AddRed(object sender, RoutedEventArgs e)
  57. {
  58. Car NewCar = new Car()
  59. {
  60. id_Teem = ((Teem)Worker.SelectedItem).id_Teem,
  61. id_Service = ((Service)TypeService.SelectedItem).id_Service,
  62. id_Client = ((Client)Client.SelectedItem).id_Client,
  63. id_CarBrand = ((CarBrand)Mark.SelectedItem).id_CarBrand,
  64. Car_mileage = Convert.ToInt32(Mileage.Text)
  65. };
  66. if(flag)
  67. {
  68. DB.SaveChanges();
  69. }
  70. else
  71. {
  72. DB.Car.Add(NewCar);
  73. DB.SaveChanges();
  74. }
  75. MessageBox.Show("Вы добавил/отредактировали машину");
  76. }
  77. private void GoBack(object sender, RoutedEventArgs e)
  78. {
  79. ClassF.Mfrm.Navigate(new CarsData());
  80. }
  81. }
  82. }