private void Update_B_Click(object sender, EventArgs e) { try { Modification modificationModel = new Modification(false); Schoolboy SchoolboyDB = Spisok_LB.SelectedItem as Schoolboy; if (SchoolboyDB == null) { throw new Exception("Необходимо выбрать объект"); } modificationModel.Averagerating_TB.Text = SchoolboyDB.Averagerating.ToString(); modificationModel.Age_TB.Text = SchoolboyDB.Age.ToString(); modificationModel.Name_TB.Text = SchoolboyDB.Name.ToString(); modificationModel.Surname_TB.Text = SchoolboyDB.Surname.ToString(); modificationModel.Middlename_TB.Text = SchoolboyDB.Middlename.ToString(); modificationModel.Class_TB.Text = SchoolboyDB.Class.ToString(); modificationModel.Ranked_TB.Text = SchoolboyDB.Ranked.ToString(); if (modificationModel.ShowDialog() == DialogResult.OK) { Schoolboy SchoolboyContext = Spisok_LB.SelectedItem as Schoolboy; SchoolboyDB.Averagerating = Convert.ToDouble(modificationModel.Averagerating_TB.Text.Trim()); SchoolboyDB.Age = Convert.ToInt32(modificationModel.Age_TB.Text.Trim()); SchoolboyDB.Name = modificationModel.Name_TB.Text.Trim(); SchoolboyDB.Surname = modificationModel.Surname_TB.Text.Trim(); SchoolboyDB.Middlename = modificationModel.Middlename_TB.Text.Trim(); SchoolboyDB.Class = Convert.ToInt32(modificationModel.Class_TB.Text.Trim()); SchoolboyDB.Ranked = Convert.ToInt32(modificationModel.Ranked_TB.Text.Trim()); context.SaveChanges(); Schoolboys.Remove(SchoolboyDB); Schoolboys.Add(SchoolboyContext); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void Del_B_Click(object sender, EventArgs e) { try { if (MessageBox.Show("Вы действительно хотите удалить?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { Schoolboy SchoolboyDB = Spisok_LB.SelectedItem as Schoolboy; if (SchoolboyDB == null) { throw new Exception("Необходимо выбрать объект"); } context.Schoolboys.Remove(SchoolboyDB); context.SaveChanges(); Schoolboys.Remove(SchoolboyDB); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }