Exemple #1
0
 private void InitializeComboBoxSages()
 {
     using (SagesAndBooksEntities context = new SagesAndBooksEntities())
     {
         comboBoxSages.Items.AddRange(context.Sages.ToArray());
     }
 }
Exemple #2
0
 private void InitializeDataGridViewSage()
 {
     using (SagesAndBooksEntities context = new SagesAndBooksEntities())
     {
         bindingSourceSage.DataSource = context.Sages.Select(s => new
         {
             Id   = s.Id,
             Name = s.Name,
             Age  = s.Age
         }).ToArray();
         dataGridViewSage.DataSource = bindingSourceSage;
     }
 }
Exemple #3
0
 private void InitializeDataGridViewBook()
 {
     using (SagesAndBooksEntities context = new SagesAndBooksEntities())
     {
         bindingSourceBook.DataSource = context.Books.Select(b => new
         {
             Id     = b.Id,
             Name   = b.Name,
             Price  = b.Price,
             SageId = b.SageId
         }).ToArray();
         dataGridViewBook.DataSource = bindingSourceBook;
     }
 }
Exemple #4
0
 private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
 {
     try
     {
         int sageId = Convert.ToInt32(dataGridViewSage.SelectedRows[0].Cells["Id"].Value);
         using (SagesAndBooksEntities context = new SagesAndBooksEntities())
         {
             Sage sage = context.Sages.Where(s => s.Id == sageId).FirstOrDefault();
             context.Sages.Remove(sage);
             context.SaveChanges();
         }
         InitializeDataGridViewSage();
     }
     catch { MessageBox.Show("Selected row == null"); }
 }
Exemple #5
0
        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            using (SagesAndBooksEntities context = new SagesAndBooksEntities())
            {
                Sage sage = context.Sages.Where(s => s.Id == sageId).FirstOrDefault();

                if (!string.IsNullOrWhiteSpace(textBoxName.Text) &&
                    !string.IsNullOrWhiteSpace(textBoxAge.Text))
                {
                    sage.Name = textBoxName.Text;
                    sage.Age  = Convert.ToInt32(textBoxAge.Text);

                    context.SaveChanges();
                }
                this.Dispose();
                this.Close();
            }
        }
Exemple #6
0
        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            using (SagesAndBooksEntities context = new SagesAndBooksEntities())
            {
                Book book = context.Books.Where(b => b.Id == bookId).FirstOrDefault();

                if (!string.IsNullOrWhiteSpace(textBoxName.Text) &&
                    !string.IsNullOrWhiteSpace(textBoxPrice.Text) && book != null)
                {
                    book.Name  = textBoxName.Text;
                    book.Price = Convert.ToInt32(textBoxPrice.Text);

                    context.SaveChanges();
                }
                this.Dispose();
                this.Close();
            }
        }
Exemple #7
0
 private void buttonInsert_Click(object sender, EventArgs e)
 {
     if (!string.IsNullOrWhiteSpace(textBoxName.Text) &&
         !string.IsNullOrWhiteSpace(textBoxAge.Text))
     {
         using (SagesAndBooksEntities context = new SagesAndBooksEntities())
         {
             Sage sage = new Sage()
             {
                 Name = textBoxName.Text,
                 Age  = Convert.ToInt32(textBoxAge.Text)
             };
             context.Sages.Add(sage);
             context.SaveChanges();
         }
         this.Dispose();
         this.Close();
     }
 }
Exemple #8
0
 private void buttonInsert_Click(object sender, EventArgs e)
 {
     if (!string.IsNullOrWhiteSpace(textBoxName.Text) &&
         !string.IsNullOrWhiteSpace(textBoxPrice.Text) &&
         !string.IsNullOrWhiteSpace(comboBoxSages.Text))
     {
         using (SagesAndBooksEntities context = new SagesAndBooksEntities())
         {
             Book book = new Book()
             {
                 Name   = textBoxName.Text,
                 Price  = Convert.ToInt32(textBoxPrice.Text),
                 SageId = (comboBoxSages.SelectedItem as Sage).Id
             };
             context.Books.Add(book);
             context.SaveChanges();
         }
         this.Dispose();
         this.Close();
     }
 }