Пример #1
0
 private void btnUpdate_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         context.SaveChanges();
         MedicineDataGrid.ClearValue(ItemsControl.ItemsSourceProperty);
         List <Medicine> medicines = context.Medicines.Include(med => med.Provider).Include(med1 => med1.Categories).ToList();
         MedicineDataGrid.ItemsSource = medicines;
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Пример #2
0
 private void btnDelete_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         MyDbContext         context             = new MyDbContext();
         MedicinesController medicinesController = new MedicinesController();
         Medicine            medicine            = MedicineDataGrid.SelectedItem as Medicine;
         if (MedicineDataGrid.SelectedItem != null)
         {
             context.Medicines.Attach(medicine);
             medicinesController.RemoveMedicine(medicine);
             MessageBox.Show("Препарат успешно удален.");
         }
         MedicineDataGrid.ClearValue(ItemsControl.ItemsSourceProperty);
         List <Medicine> medicines = context.Medicines.Include(med => med.Provider).Include(med1 => med1.Categories).ToList();
         MedicineDataGrid.ItemsSource = medicines;
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Пример #3
0
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MyDbContext         context             = new MyDbContext();
                MedicinesController medicinesController = new MedicinesController();
                if (double.TryParse(PriceMedicine.Text, out double price) == true && int.TryParse(AmmountMedicine.Text, out int ammount) == true &&
                    int.TryParse(NameMedicine.Text, out int medicineint) == false &&
                    int.TryParse(CategoriesMedicine.Text, out int categoryint) == false &&
                    int.TryParse(ProviderMedicine.Text, out int providerint) == false
                    )
                {
                    double   priceMed   = double.Parse(PriceMedicine.Text);
                    int      ammountMed = int.Parse(AmmountMedicine.Text);
                    Medicine medicine   = new Medicine(NameMedicine.Text, CategoriesMedicine.Text, ProviderMedicine.Text, priceMed, ammountMed);

                    if (medicinesController.AddMedicine(medicine))
                    {
                        MessageBox.Show("Препарат успешно добавлен.");
                        MedicineDataGrid.ClearValue(ItemsControl.ItemsSourceProperty);
                        List <Medicine> medicines = context.Medicines.Include(med => med.Provider).Include(med1 => med1.Categories).ToList();
                        MedicineDataGrid.ItemsSource = medicines;
                    }
                    else
                    {
                        MessageBox.Show("Этот препарат уже существует или введенные данные имеют неверный формат.");
                    }
                }
                else
                {
                    MessageBox.Show("Неверный формат данных");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }