private void button10_Click(object sender, EventArgs e) { addbook addb = new addbook(); DialogResult result = addb.ShowDialog(this); if (result == DialogResult.Cancel) { return; } Books boo = new Books(); boo.bookshifr = int.Parse(addb.textBox1.Text); boo.name = addb.textBox2.Text; boo.idauthor = int.Parse(addb.textBox3.Text); boo.idizdanie = int.Parse(addb.textBox4.Text); boo.idizdatelstvo = int.Parse(addb.textBox5.Text); boo.ganr = addb.textBox6.Text; boo.price = addb.textBox7.Text; db.Books.Add(boo); db.SaveChanges(); MessageBox.Show("Новый объект добавлен"); }
private void button2_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count > 0) { int index = dataGridView1.SelectedRows[0].Index; int id = 0; bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id); if (converted == false) { return; } Books boo = db.Books.Find(id); addbook addb = new addbook(); addb.textBox1.Text = boo.bookshifr.ToString(); addb.textBox2.Text = boo.name; addb.textBox3.Text = boo.idauthor.ToString(); addb.textBox4.Text = boo.idizdanie.ToString(); addb.textBox5.Text = boo.idizdatelstvo.ToString(); addb.textBox6.Text = boo.ganr; addb.textBox7.Text = boo.price; DialogResult result = addb.ShowDialog(this); if (result == DialogResult.Cancel) { return; } boo.bookshifr = int.Parse(addb.textBox1.Text); boo.name = addb.textBox2.Text; boo.idauthor = int.Parse(addb.textBox3.Text); boo.idizdanie = int.Parse(addb.textBox4.Text); boo.idizdatelstvo = int.Parse(addb.textBox5.Text); boo.ganr = addb.textBox6.Text; boo.price = addb.textBox7.Text; db.SaveChanges(); dataGridView1.Refresh(); // обновляем грид MessageBox.Show("Объект обновлен"); } }