Пример #1
0
        // Сохранение данных
        private void BSave_Click(object sender, EventArgs e)
        {
            var dateTime         = TBDateTime.Text;
            var livingСonditions = TBLivingСonditions.Text;
            var nutrition        = TBNutrition.Text;
            var population       = TBPopulation.Text;
            var key = TreeView.SelectedNode != null?int.Parse(TreeView.SelectedNode.Name) : -1;

            if (dateTime.Equals("") || livingСonditions.Equals("") || nutrition.Equals("") || population.Equals("") || key == -1)
            {
                MessageBox.Show("Укажите все параметры", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            var stat = new Statistic();

            using (var context = new AquariumContext())
            {
                switch (param)
                {
                case ActionParameters.AddStatistics:
                    stat.DateTime         = Convert.ToDateTime(dateTime);
                    stat.LivingСonditions = livingСonditions;
                    stat.Nutrition        = nutrition;
                    stat.Population       = int.Parse(population);
                    if (RBFish.Checked)
                    {
                        stat.FishId = key;
                    }
                    if (RBPlant.Checked)
                    {
                        stat.PlantId = key;
                    }
                    context.Statistics.Add(stat);
                    break;

                case ActionParameters.EditStatistics:
                    stat                  = context.Statistics.Find(selectedCells[0].Value);
                    stat.DateTime         = Convert.ToDateTime(dateTime);
                    stat.LivingСonditions = livingСonditions;
                    stat.Nutrition        = nutrition;
                    stat.Population       = int.Parse(population);
                    if (RBFish.Checked)
                    {
                        stat.FishId = key; stat.PlantId = null;
                    }
                    if (RBPlant.Checked)
                    {
                        stat.PlantId = key; stat.FishId = null;
                    }
                    break;
                }
                context.SaveChanges();
            }
            Hide();
            fsClass.InitDGVStatistics();
        }