Example #1
0
 private void btnDelete_Click(object sender, EventArgs e)
 {
     if (MessageBox.Show("Update all gridview?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
     {
         using (EFDBEntities db = new EFDBEntities())
         {
             var query = from t in db.Customers.AsEnumerable()
                         join r in cloneListCustomer.AsEnumerable()
                         on t.CustomerID equals r.CustomerID
                         select t;
             //Remove
             db.Customers.RemoveRange(query);
             //Add
             db.Customers.AddRange(cloneListCustomer);
             db.SaveChanges();
             PopulateDataGridView();
             Clear();
             MessageBox.Show("Deleted Successfully");
         }
     }
 }
Example #2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            customer_model.FirstName = txt_FirstName.Text.Trim();
            customer_model.LastName  = txt_LastName.Text.Trim();
            customer_model.City      = txt_City.Text.Trim();
            customer_model.Address   = txt_Address.Text.Trim();

            using (EFDBEntities db = new EFDBEntities())
            {
                if (customer_model.CustomerID == 0) //Insert
                {
                    db.Customers.Add(customer_model);
                }
                else //Update current row
                {
                    db.Entry(customer_model).State = System.Data.Entity.EntityState.Modified;
                }
                db.SaveChanges();
            }

            Clear();
            PopulateDataGridView();
            MessageBox.Show("Add Successfully");
        }