private void button1_Click(object sender, EventArgs e) //Save { model.Food_ID = textBox1.Text.Trim(); model.Nama = textBox2.Text.Trim(); model.Category = textBox3.Text.Trim(); model.Harga = textBox4.Text.Trim(); model.Description = textBox5.Text.Trim(); using (DBAdmin db = new DBAdmin()) { if (model.Food_ID == null) { db.FoodLists.Add(model); } else { db.Entry(model).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); } clear(); PopulateDataGridView(); MessageBox.Show("Submitted Successfully"); }
void PopulateDataGridView() { dataGridView1.AutoGenerateColumns = false; using (DBAdmin db = new DBAdmin()) { dataGridView1.DataSource = db.FoodLists.ToList <FoodList>(); } }
private void button2_Click(object sender, EventArgs e) //Delete { if (MessageBox.Show("Apakah anda yakin?", "Yes or No", MessageBoxButtons.YesNo) == DialogResult.Yes) { using (DBAdmin db = new DBAdmin()) { var entry = db.Entry(model); if (entry.State == System.Data.Entity.EntityState.Detached) { db.FoodLists.Attach(model); } db.FoodLists.Remove(model); db.SaveChanges(); PopulateDataGridView(); clear(); MessageBox.Show("Delete Successful"); } } }
private void dataGridView1_DoubleClick(object sender, EventArgs e) { if (dataGridView1.CurrentRow.Index != 0) { model.Food_ID = Convert.ToString(dataGridView1.CurrentRow.Cells[0].Value); using (DBAdmin db = new DBAdmin()) { model = db.FoodLists.Where(x => x.Food_ID == model.Food_ID).FirstOrDefault(); textBox1.Text = model.Food_ID; textBox2.Text = model.Nama; textBox3.Text = model.Category; textBox4.Text = model.Harga; textBox5.Text = model.Description; // textBox6.Text = model.Gambar; } button1.Text = "Update"; button2.Enabled = true; } }