//Add private void button3_Click(object sender, EventArgs e) { var db = new ChemistryContext(); IngredientsForm ingForm = new IngredientsForm(); List <Recipe> recipes = db.Recipes.ToList(); ingForm.comboBox1.DataSource = recipes; ingForm.comboBox1.ValueMember = "Id"; ingForm.comboBox1.DisplayMember = "Name"; DialogResult result = ingForm.ShowDialog(this); //if (result == DialogResult.Cancel) // return; Ingredient ingredient = new Ingredient(); ingredient.Name = ingForm.textBox1.Text; ingredient.Composition = ingForm.textBox2.Text; ingredient.Description = ingForm.textBox3.Text; ingredient.Amount = ingForm.textBox4.Text; ingredient.Recipe = (Recipe)ingForm.comboBox1.SelectedItem; db.Ingredients.Add(ingredient); db.SaveChanges(); MessageBox.Show("New ingredient added"); }
private void button3_Click(object sender, EventArgs e) { var db = new ChemistryContext(); 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; } Recipe recipe = db.Recipes.Find(id); RecipeForm recForm = new RecipeForm(); recForm.textBox1.Text = recipe.Name; recForm.textBox2.Text = recipe.Description; DialogResult result = recForm.ShowDialog(this); recipe.Name = recForm.textBox1.Text; recipe.Description = recForm.textBox2.Text; db.Entry(recipe).State = EntityState.Modified; db.SaveChanges(); MessageBox.Show("Updated"); } }//Dimash
private void button5_Click(object sender, EventArgs e) { var db = new ChemistryContext(); db.Ingredients.Load(); dataGridView1.DataSource = db.Ingredients.Local.ToBindingList(); }
public AllRecipes() { InitializeComponent(); var db = new ChemistryContext(); db.Recipes.Load(); dataGridView1.DataSource = db.Recipes.Local.ToBindingList(); }
public Form1() { InitializeComponent(); var db = new ChemistryContext(); db.Ingredients.Load(); dataGridView1.DataSource = db.Ingredients.Local.ToBindingList(); }
//ADd private void button4_Click(object sender, EventArgs e) { var db = new ChemistryContext(); RecipeForm recForm = new RecipeForm(); DialogResult result = recForm.ShowDialog(this); //if (result == DialogResult.Cancel) // return; Recipe recipe = new Recipe(); recipe.Name = recForm.textBox1.Text; recipe.Description = recForm.textBox2.Text; db.Recipes.Add(recipe); db.SaveChanges(); MessageBox.Show("New recipe added"); }
private void button1_Click(object sender, EventArgs e) { var db = new ChemistryContext(); 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; } Recipe recipe = db.Recipes.Find(id); listBox1.DataSource = recipe.Ingredients.ToList(); listBox1.DisplayMember = "Name"; } }
//Change private void button1_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count > 0) { var db = new ChemistryContext(); int index = dataGridView1.SelectedRows[0].Index; int id = 0; bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id); if (converted == false) { return; } Ingredient ingredient = db.Ingredients.Find(id); IngredientsForm ingForm = new IngredientsForm(); ingredient.Name = ingForm.textBox1.Text; ingredient.Composition = ingForm.textBox2.Text; ingredient.Description = ingForm.textBox3.Text; ingredient.Amount = ingForm.textBox4.Text; List <Recipe> recipes = db.Recipes.ToList(); ingForm.comboBox1.DataSource = recipes; ingForm.comboBox1.ValueMember = "Id"; ingForm.comboBox1.DisplayMember = "Name"; DialogResult result = ingForm.ShowDialog(this); //if (result == DialogResult.Cancel) // return; ingredient.Name = ingForm.textBox1.Text; ingredient.Composition = ingForm.textBox2.Text; ingredient.Description = ingForm.textBox3.Text; ingredient.Amount = ingForm.textBox4.Text; ingredient.Recipe = (Recipe)ingForm.comboBox1.SelectedItem; db.Entry(ingredient).State = EntityState.Modified; db.SaveChanges(); MessageBox.Show("Объект обновлен"); } }
//Delete private void button2_Click(object sender, EventArgs e) { var db = new ChemistryContext(); 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; } Ingredient ingredient = db.Ingredients.Find(id); db.Ingredients.Remove(ingredient); db.SaveChanges(); MessageBox.Show("Объект удален"); } }