123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- using CRUD_baseEF.Model_EF;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Xml.Linq;
- namespace CRUD_baseEF
- {
- /// <summary>
- /// Логика взаимодействия для MainWindow.xaml
- /// </summary>
- ///
- public partial class MainWindow : Window
- {
- Entities entities = new Entities();
- public MainWindow()
- {
- InitializeComponent();
- /* Передача items в ListBox */
- foreach (var item in entities.ManufacturedMachines)
- lbox_machines.Items.Add(item);
- /* Передача items в ComboBox */
- foreach (var item in entities.TypeMachine)
- cbox_type.Items.Add(item);
- }
-
- /* Отображение данных после нажатия на запись */
- private void lbox_machines_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- var selectItem = lbox_machines.SelectedItem as ManufacturedMachines;
- if (selectItem != null)
- {
- tbox_name.Text = selectItem.nameMachine;
- cbox_type.SelectedItem = (from type in entities.TypeMachine
- where type.typeMachine_ID == selectItem.typeMachine_ID
- select type).Single<TypeMachine>();
- dpic_date.Text = selectItem.makingDate;
- }
- else
- {
- tbox_name.Text = "";
- cbox_type.SelectedIndex = -1;
- dpic_date.Text = "";
- }
-
- }
- /* Изменение/Добавление данных */
- private void btn_addEdit_Click(object sender, RoutedEventArgs e)
- {
- var selectItem = lbox_machines.SelectedItem as ManufacturedMachines;
- if (tbox_name.Text == "" || cbox_type.SelectedIndex == -1 || dpic_date.Text == "")
- MessageBox.Show("Ошибка! Введите данные в поля");
- else
- {
- if (selectItem == null) // добавление новой записи
- {
- selectItem = new ManufacturedMachines();
- entities.ManufacturedMachines.Add(selectItem);
- lbox_machines.Items.Add(selectItem);
- }
- selectItem.nameMachine = tbox_name.Text;
- selectItem.typeMachine_ID = (cbox_type.SelectedItem as TypeMachine).typeMachine_ID;
- selectItem.makingDate = dpic_date.Text;
- try
- {
- entities.SaveChanges();
- lbox_machines.Items.Refresh();
- MessageBox.Show("Запись успешно сохранена");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- /* Удаление данных */
- private void btn_delete_Click(object sender, RoutedEventArgs e)
- {
- var selectItem = lbox_machines.SelectedItem as ManufacturedMachines;
- if (selectItem != null)
- {
- var resultAnswer = MessageBox.Show("Вы точно хотите удалить запись?", "Удаление",
- MessageBoxButton.YesNo, MessageBoxImage.Question);
- if (resultAnswer == MessageBoxResult.No)
- return;
- lbox_machines.Items.Remove(selectItem);
- tbox_name.Clear();
- dpic_date.Text = "";
- entities.ManufacturedMachines.Remove(selectItem);
- try
- {
- entities.SaveChanges();
- lbox_machines.Items.Refresh();
- MessageBox.Show("Запись успешно удалена");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- else MessageBox.Show("Нет удаляемых объектов", "Ошибка");
- }
- /* Сброс действий */
- private void btn_discard_Click(object sender, RoutedEventArgs e)
- {
- lbox_machines.SelectedIndex = -1;
- tbox_name.Clear();
- cbox_type.SelectedIndex = -1;
- dpic_date.Text = "";
- }
- }
- }
|