private void InitializeComboBoxSages() { using (SagesAndBooksEntities context = new SagesAndBooksEntities()) { comboBoxSages.Items.AddRange(context.Sages.ToArray()); } }
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; } }
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; } }
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"); } }
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(); } }
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(); } }
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(); } }
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(); } }