private void PopulateDataGridView() { dgvCustomer.AutoGenerateColumns = false; using (SPM_DatabaseEntities db = new SPM_DatabaseEntities()) { dgvCustomer.DataSource = db.Customers.ToList <Customer>(); } }
private void dgvCustomer_DoubleClick(object sender, EventArgs e) { if (dgvCustomer.CurrentRow.Index != -1) { model.id = Convert.ToInt32(dgvCustomer.CurrentRow.Cells["id"].Value); using (SPM_DatabaseEntities db = new SPM_DatabaseEntities()) { model = db.Customers.Where(x => x.id == model.id).FirstOrDefault(); custidtxt.Text = model.CustomerId.ToString(); custname.Text = model.Name; shortname.Text = model.ShortName; alias.Text = model.Alias; } btnSave.Text = "Update"; btnDelete.Enabled = true; } }
private void btnDelete_Click(object sender, EventArgs e) { if (MessageBox.Show("Are You Sure to Delete this Record ?", "SPM Connect", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { using (SPM_DatabaseEntities db = new SPM_DatabaseEntities()) { var entry = db.Entry(model); if (entry.State == EntityState.Detached) { db.Customers.Attach(model); } db.Customers.Remove(model); db.SaveChanges(); PopulateDataGridView(); Clear(); MessageBox.Show("Deleted Successfully", "SPM Connect", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
private void btnSave_Click(object sender, EventArgs e) { model.CustomerId = Convert.ToInt16(custidtxt.Text.ToString()); model.Name = custname.Text.Trim(); model.ShortName = shortname.Text.Trim(); model.Alias = alias.Text.Trim(); using (SPM_DatabaseEntities db = new SPM_DatabaseEntities()) { if (model.id == 0)//Insert { db.Customers.Add(model); } else //Update { db.Entry(model).State = EntityState.Modified; } db.SaveChanges(); } Clear(); PopulateDataGridView(); MessageBox.Show("Submitted Successfully", "SPM Connect", MessageBoxButtons.OK, MessageBoxIcon.Information); }