コード例 #1
0
        // verified
        public void Delete()
        {
            if (TableV.Current_DataGrid.SelectedItems.Count < 1)
            {
                MessageBox.Show("Выберите элементы для удаления!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            while (TableV.Current_DataGrid.SelectedItems.Count > 0)
            {
                index = TableV.Current_DataGrid.SelectedIndex;

                LocomotivesM deleteEntity = SourceList[index];

                var trains = (from o in dbContext.TrainsMs
                              where o._locomotive_id == deleteEntity._locomotive_id
                              select o);

                if (trains != null)
                {
                    foreach (var train in trains)
                    {
                        var schedules = (from o in dbContext.SchedulesMs
                                         where o._train_id == train._train_id
                                         select o);

                        if (schedules != null)
                        {
                            foreach (var schedule in schedules)
                            {
                                var tickets = (from o in dbContext.TicketsMs
                                               where o._schedule_id == schedule._schedule_id
                                               select o);

                                if (tickets != null)
                                {
                                    dbContext.TicketsMs.RemoveRange(tickets);
                                }
                            }

                            dbContext.SchedulesMs.RemoveRange(schedules);
                        }
                    }

                    dbContext.TrainsMs.RemoveRange(trains);
                }

                dbContext.LocomotivesMs.Local.Remove(deleteEntity);

                // Удаляем НЕконтекстного юзера из SourceList
                SourceList.Remove(deleteEntity);
            }

            // Сохраняем контекст БД
            dbContext.SaveChanges();

            TableV.Current_DataGrid.ItemsSource = SourceList;
            TableV.Current_DataGrid.Items.Refresh();
        }
コード例 #2
0
        // Verified
        public void ExecuteAddEdit(object sender, RoutedEventArgs e)
        {
            // Проверки TextBox на null и пустую строку
            if (model.Text == null || model.Text == "")
            {
                MessageBox.Show("Укажите модель!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (type.Text == null || type.Text == "")
            {
                MessageBox.Show("Укажите тип!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            double result;

            // Проверки TextBox на null и пустую строку
            if (weight.Text == null || weight.Text == "")
            {
                MessageBox.Show("Укажите вес!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else if (!double.TryParse(weight.Text, out result))
            {
                MessageBox.Show("Некорректная вес!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (maxTrailerWeight.Text == null || maxTrailerWeight.Text == "")
            {
                MessageBox.Show("Укажите максимально допустимую массу прицепа!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else if (!double.TryParse(weight.Text, out result))
            {
                MessageBox.Show("Некорректная максимально допустимая масса прицепа!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (avgspeed0.Text == null || avgspeed0.Text == "")
            {
                MessageBox.Show("Укажите среднюю скорость при нулевом грузе!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else if (!double.TryParse(avgspeed0.Text, out result))
            {
                MessageBox.Show("Некорректная средняя скорость!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (avgspeed100.Text == null || avgspeed100.Text == "")
            {
                MessageBox.Show("Укажите среднюю скорость при максимальном грузе!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else if (!double.TryParse(avgspeed100.Text, out result))
            {
                MessageBox.Show("Некорректная средняя скорость!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            var unique = (from o in dbContext.LocomotivesMs
                          where o._model == model.Text
                          select o).FirstOrDefault();

            if (unique != null && (isAdd || unique._locomotive_id != SourceList[index]._locomotive_id))
            {
                MessageBox.Show("Данный локомотив уже присутствует в таблице!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            LocomotivesM temp = new LocomotivesM();

            temp._model              = model.Text;
            temp._type               = type.SelectedIndex;
            temp._weight             = double.Parse(weight.Text);
            temp._max_trailer_weight = double.Parse(maxTrailerWeight.Text);
            temp._avgspeed0          = double.Parse(avgspeed0.Text);
            temp._avgspeed100        = double.Parse(avgspeed100.Text);

            // Сохранение нового юзера в БД
            if (isAdd)
            {
                // Добавляем объект в БД
                dbContext.LocomotivesMs.Local.Add(temp);

                // Очищаем поля
                model.Text            = null;
                type.SelectedIndex    = 0;
                weight.Text           = null;
                maxTrailerWeight.Text = null;
                avgspeed0.Text        = null;
                avgspeed100.Text      = null;
            }
            else
            {
                // Получаем объект из БД по айди, который будем изменять
                int id   = SourceList[index]._locomotive_id;
                var loco = dbContext.LocomotivesMs.Local
                           .Single(o => o._locomotive_id == id);

                // Изменяем, просто изменяя поля на поля объекта temp
                loco._model              = temp._model;
                loco._type               = temp._type;
                loco._weight             = temp._weight;
                loco._max_trailer_weight = temp._max_trailer_weight;
                loco._avgspeed0          = temp._avgspeed0;
                loco._avgspeed100        = temp._avgspeed100;

                // Говорим контексту БД, что данный объект был изменен
                dbContext.Entry(loco).State = EntityState.Modified;
            }
            dbContext.SaveChanges();

            // Обновление списка
            SourceList = dbContext.LocomotivesMs.Local.ToBindingList();

            TableV.Current_DataGrid.ItemsSource = SourceList;
            TableV.Current_DataGrid.Items.Refresh();
        }
コード例 #3
0
        // Verified
        public void AddEdit(bool isAdd)
        {
            this.isAdd = isAdd;
            // Показываем диалоговое окно
            DialogV dialogV    = new DialogV();
            Grid    dialogGrid = dialogV.Dialog_Grid;

            Button button = dialogV.Button_Execute;

            // Если это добавление
            if (isAdd)
            {
                button.Content = App.Current.Resources["Text_Add"];
                index          = -1;
            }
            // Если это изменение
            else
            {
                // если ничего не выбрано в датагриде то ошибка
                // если выбрано больше 1 элемента то тоже ошибка
                if (TableV.Current_DataGrid.SelectedItems.Count < 1)
                {
                    MessageBox.Show("Выберите элемент для изменения!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                else if (TableV.Current_DataGrid.SelectedItems.Count > 1)
                {
                    MessageBox.Show("Можно выбрать для изменения не более ОДНОГО элемента за раз!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                // индекс текущей выбранной строки в DataGrid
                // кастыль, но куда без кастылей?
                index = TableV.Current_DataGrid.SelectedIndex;

                // Если всё ок, вставляем данные для данного юзера в форму, который затем будем менять
                LocomotivesM temp = SourceList[index];

                model.Text            = temp._model;
                type.SelectedIndex    = temp._type;
                weight.Text           = temp._weight.ToString();
                maxTrailerWeight.Text = temp._max_trailer_weight.ToString();
                avgspeed0.Text        = temp._avgspeed0.ToString();
                avgspeed100.Text      = temp._avgspeed100.ToString();

                button.Content = App.Current.Resources["Text_Edit"];
            }

            button.Click += new RoutedEventHandler(ExecuteAddEdit);

            // Вешаем элементы в Grid
            dialogGrid.Children.Add(Model);
            dialogGrid.Children.Add(Type);
            dialogGrid.Children.Add(Weight);
            dialogGrid.Children.Add(MaxTrailerWeight);
            dialogGrid.Children.Add(AvgSpeed0);
            dialogGrid.Children.Add(AvgSpeed100);

            dialogGrid.Children.Add(model);
            dialogGrid.Children.Add(type);
            dialogGrid.Children.Add(weight);
            dialogGrid.Children.Add(maxTrailerWeight);
            dialogGrid.Children.Add(avgspeed0);
            dialogGrid.Children.Add(avgspeed100);

            // Заполняем нижний Button текстом и вешаем локальный обработчик события нажатия

            dialogV.ShowDialog();

            // Очищаем Grid
            dialogGrid.Children.Remove(model);
            dialogGrid.Children.Remove(type);
            dialogGrid.Children.Remove(weight);
            dialogGrid.Children.Remove(maxTrailerWeight);
            dialogGrid.Children.Remove(avgspeed0);
            dialogGrid.Children.Remove(avgspeed100);

            dialogGrid.Children.Remove(Model);
            dialogGrid.Children.Remove(Type);
            dialogGrid.Children.Remove(Weight);
            dialogGrid.Children.Remove(MaxTrailerWeight);
            dialogGrid.Children.Remove(AvgSpeed0);
            dialogGrid.Children.Remove(AvgSpeed100);

            model.Text            = null;
            type.SelectedIndex    = 0;
            weight.Text           = null;
            maxTrailerWeight.Text = null;
            avgspeed0.Text        = null;
            avgspeed100.Text      = null;
        }