public Edit(Sight sight, string con, bool view = false) { try { connectionString = con; if (sight != null) { if (!view) { this.sight = sight; InitializeComponent(); InitForm(sight); AuthorComboBox.Visible = true; } else { this.sight = sight; InitializeComponent(); InitForm(sight); NoVisible(); SaveButton.Visible = false; DelAuthButton.Enabled = false; AuthorComboBox.Enabled = false; DelButton.Visible = false; AuthorDGV.Visible = false; AuthorLabel1.Visible = false; this.Text = "Просмотр"; } } else { this.sight = new Sight(); InitializeComponent(); InitForm(); SaveButton.Text = "Создать"; this.Text = "Создание"; DelButton.Visible = false; AuthorComboBox.Visible = true; DelAuthButton.Visible = false; AuthorDataGridView.Visible = false; AuthorLabel.Visible = false; } } catch (DbUpdateException) { MessageBox.Show(text: "У вас нет прав на это действие.", caption: "Error", buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error); } }
private void InitForm(Sight sight) { try { using (SightOfOneCityEntities context = new SightOfOneCityEntities(connectionString)) { NameTextBox.Text = sight.NameSight; DiscriptionTextBox.Text = sight.DescriptionSight; AddressTextBox.Text = sight.AddresSights; YearNumericUpDown.Value = (from sig in context.Sight join aos in context.AuthorOfSight on sig.ID equals aos.SightID where sig.ID == sight.ID select aos.DateCreate).FirstOrDefault(); YearNumericUpDown.ValueChanged += YearNumericUpDown_ValueChanged; if (File.Exists(sight.Photo.Trim())) { Image imgsight = Image.FromFile(sight.Photo.Trim()); PictureBox.Image = imgsight; PictureBox.SizeMode = PictureBoxSizeMode.StretchImage; } else { Image imgsight = Image.FromFile("unnamed.png"); PictureBox.Image = imgsight; PictureBox.SizeMode = PictureBoxSizeMode.StretchImage; } AddPictureButton.Text = "Изменить изображение"; this.Text = "Изменение"; StatusComboBox.DataSource = (from status in context.StatusSight select status.NameStatus).ToList(); StatusComboBox.TextChanged += StatusComboBox_TextChanged; StatusComboBox.Text = (from status in context.StatusSight where status.ID == sight.StatusID select status.NameStatus).FirstOrDefault().ToString(); StopComboBox.DataSource = (from stop in context.TransportStop select stop.NameStop).ToList(); StopComboBox.TextChanged += StopComboBox_TextChanged; StopComboBox.Text = (from sig in context.Sight join stop in context.TransportStop on sig.StopID equals stop.ID where sig.ID == sight.ID select stop.NameStop).Single(); CategoryComboBox.DataSource = (from category in context.Category select category.NameCategory).ToList(); CategoryComboBox.TextChanged += CategoryComboBox_TextChanged; CategoryComboBox.Text = (from sig in context.Sight join category in context.Category on sig.CategoryID equals category.ID where sig.ID == sight.ID select category.NameCategory).Single(); if (this.Text == "Изменение") { AuthorComboBox.DataSource = (from author in context.Author select author.FamilyAuthor + " " + author.NameAuthor + " " + author.PatronymicAuthor).ToList(); AuthorComboBox.SelectedItem = null; AuthorComboBox.TextChanged += AuthorComboBox_TextChanged; } InitDGV(); } } catch (DbUpdateException) { MessageBox.Show(text: "У вас нет прав на это действие.", caption: "Error", buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error); } }
private void SaveButton_Click(object sender, EventArgs e) { try { if (CheckFied()) { using (SightOfOneCityEntities context = new SightOfOneCityEntities(connectionString)) { if (this.Text == "Изменение") { Sight s = context.Sight.Find((from sig in context.Sight where sig.ID == sight.ID select sig.ID).Single()); s.NameSight = sight.NameSight; s.DescriptionSight = sight.DescriptionSight; s.AddresSights = sight.AddresSights; s.Photo = sight.Photo; s.StopID = sight.StopID; s.StatusID = sight.StatusID; s.CategoryID = sight.CategoryID; for (int i = 0; i < AuthorDGV.Rows.Count; i++) { string namea = AuthorDGV.Rows[i].Cells[1].Value.ToString(); string familya = AuthorDGV.Rows[i].Cells[0].Value.ToString(); string patrona = AuthorDGV.Rows[i].Cells[2].Value.ToString(); context.AuthorOfSight.Add(new AuthorOfSight(sight.ID, (from author in context.Author where author.NameAuthor == namea && author.FamilyAuthor == familya && author.PatronymicAuthor == patrona select author.ID).FirstOrDefault(), (short)YearNumericUpDown.Value)); } context.SaveChanges(); } else { context.Sight.Add(new Sight(sight.NameSight, sight.DescriptionSight, sight.Photo, sight.AddresSights, sight.StopID, sight.StatusID, sight.CategoryID)); for (int i = 0; i < AuthorDGV.Rows.Count; i++) { string namea = AuthorDGV.Rows[i].Cells[1].Value.ToString(); string familya = AuthorDGV.Rows[i].Cells[0].Value.ToString(); string patrona = AuthorDGV.Rows[i].Cells[2].Value.ToString(); context.AuthorOfSight.Add(new AuthorOfSight((from sig in context.Sight where sig.NameSight == NameTextBox.Text select sig.ID).FirstOrDefault(), (from author in context.Author where author.NameAuthor == namea && author.FamilyAuthor == familya && author.PatronymicAuthor == patrona select author.ID).FirstOrDefault(), (short)YearNumericUpDown.Value)); } context.SaveChanges(); } } this.Close(); } else { MessageBox.Show("Не все обязательные поля заполнены. Проверьте данные.", "Внимание"); } } catch (DbUpdateException) { MessageBox.Show(text: "У вас нет прав на это действие.", caption: "Error", buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error); } }