private void btnDelete_Click(object sender, EventArgs e) { using (Library2Entities db = new Library2Entities()) { if (tabBox.SelectedTab.Name == "tabBooks") { if (dataGridBooks.SelectedRows.Count > 0) { int index = dataGridBooks.SelectedRows[0].Index; int id = Convert.ToInt32(dataGridBooks[0, index].Value); Book book = db.Book.Find(id); db.Book.Remove(book); db.SaveChanges(); GetAll(dataGridBooks); } } else if (tabBox.SelectedTab.Name == "tabAuthors") { if (dataGridAuthors.SelectedRows.Count > 0) { int index = dataGridAuthors.SelectedRows[0].Index; int id = Convert.ToInt32(dataGridAuthors[0, index].Value); Author author = db.Author.Find(id); db.Author.Remove(author); db.SaveChanges(); GetAll(dataGridAuthors); } } else if (tabBox.SelectedTab.Name == "tabPublishers") { if (dataGridPublishers.SelectedRows.Count > 0) { int index = dataGridPublishers.SelectedRows[0].Index; int id = Convert.ToInt32(dataGridPublishers[0, index].Value); Publisher publisher = db.Publisher.Find(id); db.Publisher.Remove(publisher); db.SaveChanges(); GetAll(dataGridPublishers); } } } }
private void btnAdd_Click(object sender, EventArgs e) { using (Library2Entities db = new Library2Entities()) { if (tabBox.SelectedTab.Name == "tabBooks") { int index = dataGridBooks.CurrentRow.Index; Book book = new Book(); book.Title = dataGridBooks["Title", index].Value.ToString(); book.IdAuthor = Convert.ToInt32(dataGridBooks["IdAuthor", index].Value); book.Pages = Convert.ToInt32(dataGridBooks["Pages", index].Value); book.Price = Convert.ToInt32(dataGridBooks["Price", index].Value); book.IdPublisher = Convert.ToInt32(dataGridBooks["IdPublisher", index].Value); db.Book.Add(book); db.SaveChanges(); GetAll(dataGridBooks); } else if (tabBox.SelectedTab.Name == "tabAuthors") { int index = dataGridAuthors.CurrentRow.Index; Author author = new Author(); author.FirstName = dataGridAuthors["FirstName", index].Value.ToString(); author.LastName = dataGridAuthors["LastName", index].Value.ToString(); db.Author.Add(author); db.SaveChanges(); GetAll(dataGridAuthors); } else if (tabBox.SelectedTab.Name == "tabPublishers") { int index = dataGridPublishers.CurrentRow.Index; Publisher publisher = new Publisher(); publisher.PublisherName = dataGridPublishers["PublisherName", index].Value.ToString(); publisher.Address = dataGridPublishers["Address", index].Value.ToString(); db.Publisher.Add(publisher); db.SaveChanges(); GetAll(dataGridPublishers); } } }
private void btnEdit_Click(object sender, EventArgs e) { using (Library2Entities db = new Library2Entities()) { if (tabBox.SelectedTab.Name == "tabBooks") { int index = dataGridBooks.CurrentRow.Index; int id = Convert.ToInt32(dataGridBooks[0, index].Value); db.Book.Find(id).Title = dataGridBooks["Title", index].Value.ToString(); db.Book.Find(id).IdAuthor = Convert.ToInt32(dataGridBooks["IdAuthor", index].Value); db.Book.Find(id).Pages = Convert.ToInt32(dataGridBooks["Pages", index].Value); db.Book.Find(id).Price = Convert.ToInt32(dataGridBooks["Price", index].Value); db.Book.Find(id).IdPublisher = Convert.ToInt32(dataGridBooks["IdPublisher", index].Value); db.SaveChanges(); GetAll(dataGridBooks); } else if (tabBox.SelectedTab.Name == "tabAuthors") { int index = dataGridAuthors.CurrentRow.Index; int id = Convert.ToInt32(dataGridAuthors[0, index].Value); db.Author.Find(id).FirstName = dataGridAuthors["FirstName", index].Value.ToString(); db.Author.Find(id).LastName = dataGridAuthors["LastName", index].Value.ToString(); db.SaveChanges(); GetAll(dataGridAuthors); } else if (tabBox.SelectedTab.Name == "tabPublishers") { int index = dataGridPublishers.CurrentRow.Index; int id = Convert.ToInt32(dataGridPublishers[0, index].Value); db.Publisher.Find(id).PublisherName = dataGridPublishers["PublisherName", index].Value.ToString(); db.Publisher.Find(id).Address = dataGridPublishers["Address", index].Value.ToString(); db.SaveChanges(); GetAll(dataGridPublishers); } } }