private void btnAdd_Click(object sender, EventArgs e) { try { using (var context = new ProductType1Entities()) { var model_ProductType = new ProductType(); string Name = txtName.Text; model_ProductType.Name = txtName.Text; model_ProductType.Remark = txtRemark.Text; var model = context.ProductTypes.Where(a => a.Name == Name).FirstOrDefault(); if (model == null) { context.ProductTypes.Add(model_ProductType); } else { var entry = context.Entry(model); entry.CurrentValues.SetValues(model_ProductType); entry.State = System.Data.Entity.EntityState.Modified; } context.SaveChanges(); Load_Data_Grid(); } MessageBox.Show("Product Added Successfully!"); } catch (Exception ee) { MessageBox.Show(ee.Message); } }
private void PopulateDataGridView() { { dgvproducttype.AutoGenerateColumns = false; } using (ProductType1Entities db = new ProductType1Entities()) { dgvproducttype.DataSource = db.ProductTypes.ToList <ProductType>(); } }
private void btnDelete_Click_1(object sender, EventArgs e) { using (var context = new ProductType1Entities()) { var model_ProductType = new ProductType(); string Name = txtName.Text; var model = context.ProductTypes.Where(a => a.Name == Name).FirstOrDefault(); if (model != null) { context.ProductTypes.Remove(model); context.SaveChanges(); PopulateDataGridView(); MessageBox.Show("Product Deleted Successfully"); } } }
private void dgvproducttype_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { DataGridViewRow row = this.dgvproducttype.Rows[e.RowIndex]; var id = (int)row.Cells["ID"].Value; if (id > 0) { using (ProductType1Entities db = new ProductType1Entities()) { var model = db.ProductTypes.Where(x => x.ID == id).FirstOrDefault(); txtName.Text = model.Name; txtRemark.Text = model.Remark; } btnAdd.Text = "Update"; btnDelete.Enabled = true; } }