private void Show() { dataGridView1.Rows.Clear(); var context = new LibraryEntities(); foreach (var books in context.BOOKS) { dataGridView1.Rows.Add(books.Id.ToString(), books.Author.FirstName, books.Author.LatsName, books.Tittle, books.Price); } }
private void dataGridView1_MouseClick(object sender, MouseEventArgs e) { if (dataGridView1.CurrentRow.Index != -1) { bookModel.Id = Convert.ToInt32(dataGridView1.CurrentRow.Cells["ID"].Value); using (LibraryEntities db = new LibraryEntities()) { authorModel = db.Authors.Where(x => x.Id == bookModel.Id).FirstOrDefault(); bookModel = db.BOOKS.Where(x => x.Id == bookModel.Id).FirstOrDefault(); textBoxAuthor_FirstName.Text = authorModel.FirstName; textBoxAuthor_SecondName.Text = authorModel.LatsName; textBoxBook_title.Text = bookModel.Tittle; textBoxPrice.Text = (bookModel.Price).ToString(); } buttonSave.Text = "Update"; buttonDelete.Enabled = false; } buttonDelete.Enabled = true; }
private void buttonSave_Click(object sender, EventArgs e) { bookModel.Tittle = textBoxBook_title.Text.Trim(); bookModel.Price = Convert.ToInt32(textBoxPrice.Text); authorModel.FirstName = textBoxAuthor_FirstName.Text.Trim(); authorModel.LatsName = textBoxAuthor_SecondName.Text.Trim(); using (LibraryEntities db = new LibraryEntities()) { if (authorModel.Id == 0 && bookModel.Id == 0) { db.Authors.Add(authorModel); db.BOOKS.Add(bookModel); } else { db.Entry(bookModel).State = System.Data.Entity.EntityState.Modified; db.Entry(authorModel).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); Show(); } }
private void buttonDelete_Click(object sender, EventArgs e) { if (MessageBox.Show("Are you sure to delete?", "Process to delete", MessageBoxButtons.YesNo) == DialogResult.Yes) { using (LibraryEntities db = new LibraryEntities()) { var contextBook = db.Entry(bookModel); var contextAuthor = db.Entry(authorModel); if ((contextAuthor.State == System.Data.Entity.EntityState.Detached) && (contextBook.State == System.Data.Entity.EntityState.Detached)) { db.Authors.Attach(authorModel); db.BOOKS.Attach(bookModel); } db.Authors.Remove(authorModel); db.BOOKS.Remove(bookModel); db.SaveChanges(); Show(); } } }