private void Button1_Click_1(object sender, EventArgs e) { SpecialtyChange tmForm = new SpecialtyChange(); DialogResult result = tmForm.ShowDialog(this); if (result == DialogResult.Cancel) { return; } Specialty specialty = new Specialty(); specialty.Name = tmForm.textBox1.Text; db.Specialties.Add(specialty); db.SaveChanges(); MessageBox.Show("Новый объект добавлен"); }
// добавление private void button1_Click(object sender, EventArgs e) { SpecialtyChange tmForm = new SpecialtyChange(); List <Faculty> faculties = db.Faculties.ToList(); textBox1.Text = faculties.ToString(); tmForm.comboBox1.DataSource = faculties; tmForm.comboBox1.ValueMember = "Id"; tmForm.comboBox1.DisplayMember = "Name"; DialogResult result = tmForm.ShowDialog(this); if (result == DialogResult.Cancel) { return; } Specialty specialty = new Specialty(); specialty.Name = tmForm.textBox1.Text; specialty.Faculty = (Faculty)tmForm.comboBox1.SelectedItem; db.Specialties.Add(specialty); db.SaveChanges(); MessageBox.Show("Новый объект добавлен"); }