Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. DB db { get; set; }
  2. public MainWindow()
  3. {
  4. InitializeComponent();
  5. using (DB dbContext = new DB())
  6. {
  7. dbContext.Database.Migrate();
  8. }
  9.  
  10. db = new DB();
  11. }
  12. private void Window_Loaded(object sender, RoutedEventArgs e)
  13. {
  14. dgDatiAziende.ItemsSource = db.Aziende.ToList();
  15. dgDatiStudenti.ItemsSource = db.Studenti.ToList();
  16. }
  17.  
  18. private void Studente_Click(object sender, RoutedEventArgs e)
  19. {
  20. formStudente f = new formStudente();
  21. f.ShowDialog();
  22.  
  23. if (f.Studente != null)
  24. {
  25. db.Studenti.Add(f.Studente);
  26. db.SaveChanges();
  27.  
  28. dgDatiStudenti.ItemsSource = db.Studenti.ToList();
  29. }
  30. }
  31.  
  32. private void Azienda_Click(object sender, RoutedEventArgs e)
  33. {
  34. formAzienda fa = new formAzienda();
  35. fa.ShowDialog();
  36.  
  37. if (fa.Azienda != null)
  38. {
  39. db.Aziende.Add(fa.Azienda);
  40. db.SaveChanges();
  41.  
  42. dgDatiAziende.ItemsSource = db.Aziende.ToList();
  43. }
  44. }
  45.  
  46. private void Window_Closed(object sender, EventArgs e)
  47. {
  48.  
  49. }
  50.  
  51. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  52. {
  53.  
  54. }
  55.  
  56. private void Importa_studente_Click(object sender, RoutedEventArgs e)
  57. {
  58. Studente importato = null;
  59.  
  60. try
  61. {
  62. StreamReader fileIn = new StreamReader("Studenti.csv");
  63. string riga = fileIn.ReadLine();
  64. string[] colonne = riga.Split(';');
  65. int ncolonne = colonne.Length;
  66. while (!fileIn.EndOfStream)
  67. {
  68. try
  69. {
  70. riga = fileIn.ReadLine();
  71. colonne = riga.Split(';');
  72.  
  73. importato = new Studente
  74. {
  75. Nome = colonne[0],
  76. Cognome = colonne[1],
  77. Residenza = colonne[2],
  78. Data_Nascita = colonne[3],
  79. Classe = colonne[4],
  80. Cf = colonne[5]
  81. };
  82. }
  83. catch (Exception Errore)
  84. {
  85. MessageBox.Show(Errore.Message);
  86. }
  87. }
  88. }
  89. catch (Exception Errore)
  90. {
  91. MessageBox.Show(Errore.Message);
  92. }
  93.  
  94. if (importato != null)
  95. {
  96. db.Studenti.Add(importato);
  97. db.SaveChanges();
  98.  
  99. dgDatiStudenti.ItemsSource = db.Studenti.ToList();
  100. }
  101. }
  102.  
  103. private void Importa_azienda_Click(object sender, RoutedEventArgs e)
  104. {
  105. ImportazioneAziende ia = new ImportazioneAziende();
  106. ia.ShowDialog();
  107. if (ia.Azienda != null)
  108. {
  109. db.Aziende.Add(ia.Azienda);
  110. db.SaveChanges();
  111.  
  112. dgDatiAziende.ItemsSource = db.Aziende.ToList();
  113. }
  114. }
  115.  
  116. private void DeleteStudente_Click(object sender, RoutedEventArgs e)
  117. {
  118. Studente s = dgDatiStudenti.SelectedItem as Studente;
  119. if (s == null)
  120. {
  121. MessageBox.Show("Prima devi selezionare uno studente!");
  122. }
  123. else
  124. {
  125. Studente sbuono = db.Studenti.Find(s.ID);
  126. db.Studenti.Remove(sbuono);
  127. db.SaveChanges();
  128.  
  129. dgDatiStudenti.ItemsSource = db.Studenti.ToList();
  130. }
  131. }
  132.  
  133. private void EditStudente_Click(object sender, RoutedEventArgs e)
  134. {
  135. // Prelevo lo studente dal dataGrid
  136. Studente s = dgDatiStudenti.SelectedItem as Studente;
  137.  
  138. Studente sbuono = db.Studenti.Find(s.ID);
  139.  
  140. if (sbuono != null)
  141. {
  142. formStudente f = new formStudente(sbuono, "Accetta modifiche");
  143. f.ShowDialog();
  144.  
  145. if (f.Studente != null)
  146. {
  147. db.Studenti.Add(f.Studente);
  148. db.SaveChanges();
  149.  
  150. dgDatiStudenti.ItemsSource = db.Studenti.ToList();
  151.  
  152.  
  153. sbuono.Cognome = "Pippo";
  154. db.SaveChanges();
  155. dgDatiStudenti.ItemsSource = db.Studenti.ToList();
  156. }
  157.  
  158. }
  159. }
  160.  
  161. private void DeleteAzienda_Click(object sender, RoutedEventArgs e)
  162. {
  163. Azienda fa = dgDatiAziende.SelectedItem as Azienda;
  164. if (fa == null)
  165. {
  166. MessageBox.Show("Prima devi selezionare uno studente!");
  167. }
  168. else
  169. {
  170. Azienda abuono = db.Aziende.Find(fa.AziendaID);
  171. db.Aziende.Remove(abuono);
  172. db.SaveChanges();
  173.  
  174. dgDatiAziende.ItemsSource = db.Aziende.ToList();
  175. }
  176. }
  177.  
  178. private void EditAzienda_Click(object sender, RoutedEventArgs e)
  179. {
  180. Azienda fa = dgDatiAziende.SelectedItem as Azienda;
  181.  
  182. Azienda abuono = db.Aziende.Find(fa.AziendaID);
  183.  
  184. if (abuono != null)
  185. {
  186. formAzienda a = new formAzienda(abuono, "Accetta modifiche");
  187. a.ShowDialog();
  188.  
  189. if (a.Azienda != null)
  190. {
  191. db.Aziende.Add(a.Azienda);
  192. db.SaveChanges();
  193.  
  194. dgDatiAziende.ItemsSource = db.Aziende.ToList();
  195.  
  196.  
  197. abuono.Nome = "Pippo";
  198. db.SaveChanges();
  199. dgDatiAziende.ItemsSource = db.Aziende.ToList();
  200. }
  201.  
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement