Exemplo n.º 1
0
        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;
                Book book = db.Books.Find(id);

                AddBooks addBooks = new AddBooks();

                addBooks.textBox1.Text = book.name;
                addBooks.comboBox1.Text = listGenre.Find(ob => ob.id_genre == book.id_genre).genre1;
                addBooks.comboBox2.Text = listAuthor.Find(ob => ob.id_author == book.id_author).firstname;
               
                DialogResult result = addBooks.ShowDialog(this);

                if (result == DialogResult.Cancel)
                    Hide();

                book.name = addBooks.textBox1.Text;
                book.id_genre = listGenre.Find(ob => ob.genre1 == addBooks.comboBox1.Text).id_genre;
                book.id_author = listAuthor.Find(ob => ob.firstname == addBooks.comboBox2.Text).id_author;
                db.SaveChanges();
                dataGridView1.Refresh();
                Refresh();
                MessageBox.Show("Объект обновлен");
            }
        }
Exemplo n.º 2
0
        private void button1_Click(object sender, EventArgs e)
        {

            AddBooks addBooks = new AddBooks();

            DialogResult result = addBooks.ShowDialog(this);
            Book book = new Book();
            if (result == DialogResult.Cancel)
                Hide();
            if (result == DialogResult.OK)
            {
                
                
                book.name = addBooks.textBox1.Text;
                book.id_genre = listGenre.Find(ob => ob.genre1 == addBooks.comboBox1.Text).id_genre;
                book.id_author = listAuthor.Find(ob => ob.firstname == addBooks.comboBox2.Text).id_author;
                //db_lab4Entities db = new db_lab4Entities();
                db.Books.Add(book);
                db.SaveChanges();
                
                dataGridView1.Refresh();
                Refresh();
                MessageBox.Show("Добавлена новая книга!");
            }
        }