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
//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"); }
//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"); }
//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("Объект удален"); } }