Example #1
0
 private void RemoveBookButton_Click(object sender, EventArgs e)
 {
     if (BooksToEditGrid.SelectedRows.Count == 0)
     {
         return;
     }
     using (LibraryDBEntities dBContext = new LibraryDBEntities()) {
         List <Book> books = dBContext.Books.ToList();
         int         id    = Convert.ToInt32(BooksToEditGrid.SelectedRows[0].Cells[5].Value);
         foreach (Book book in books)
         {
             if (book.Book_Id == id)
             {
                 dBContext.Books.Remove(book);
             }
         }
         dBContext.SaveChanges();
         DataGridViewRow rowToDelete = BooksToEditGrid.SelectedRows[0];
         BooksToEditGrid.Rows.Remove(rowToDelete);
         BooksToEditGrid.ClearSelection();
     }
 }
Example #2
0
        private void UpdateBookButton_Click(object sender, EventArgs e)
        {
            bool   isChanged          = false;
            string newBookTitle       = EditBookTitleTextbox.Text;
            string newBookAutor       = EditAutorTextbox.Text;
            string newBookGenre       = EditGenreTextbox.Text;
            string newBookYear        = EditYearTextbox.Text;
            string newBookPublication = EditPublicationTextbox.Text;

            using (LibraryDBEntities dbContex = new LibraryDBEntities()) {
                if (BooksToEditGrid.SelectedRows.Count == 0)
                {
                    return;
                }
                int  id   = Convert.ToInt32(BooksToEditGrid.SelectedRows[0].Cells[5].Value);
                Book book = dbContex.Books.SingleOrDefault(b => b.Book_Id == id);
                if (book != null)
                {
                    if (newBookTitle != null && newBookTitle.Trim().Length > 0)
                    {
                        book.Name = newBookTitle;
                        BooksToEditGrid.SelectedRows[0].Cells[0].Value = newBookTitle;
                    }
                    if (newBookAutor != null && newBookAutor.Trim().Length > 0)
                    {
                        book.Autor = newBookAutor;
                        BooksToEditGrid.SelectedRows[0].Cells[1].Value = newBookAutor;
                    }
                    if (newBookGenre != null && newBookGenre.Trim().Length > 0)
                    {
                        book.Genre = newBookGenre;
                        BooksToEditGrid.SelectedRows[0].Cells[2].Value = newBookGenre;
                    }
                    if (newBookYear != null && newBookYear.Trim().Length > 0)
                    {
                        int num;
                        if (!int.TryParse(EditYearTextbox.Text, out num))
                        {
                            MessageBox.Show("Incorrect year!", "Error");
                            return;
                        }
                        book.Year = newBookYear;
                        BooksToEditGrid.SelectedRows[0].Cells[3].Value = newBookYear;
                    }
                    if (newBookPublication != null && newBookPublication.Trim().Length > 0)
                    {
                        book.Publication = newBookPublication;
                        BooksToEditGrid.SelectedRows[0].Cells[4].Value = newBookPublication;
                    }
                    isChanged = true;
                    dbContex.SaveChanges();
                }
                BooksToEditGrid.Refresh();
                EditBookTitleTextbox.Clear();
                EditAutorTextbox.Clear();
                EditGenreTextbox.Clear();
                EditYearTextbox.Clear();
                EditPublicationTextbox.Clear();
                if (isChanged)
                {
                    MessageBox.Show("Information changed successfuly", "Success", MessageBoxButtons.OK);
                }
            }
        }