Exemple #1
0
 private void btnDelete_Click(object sender, EventArgs e)
 {
     if (btnUpdate.Enabled == false)
     {
         btnAdd.Enabled    = true;
         btnSave.Enabled   = false;
         btnUpdate.Enabled = true;
         btnDelete.Text    = "Xóa";
     }
     else
     {
         var ques = MessageBox.Show("Xác nhận xóa?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
         if (ques == DialogResult.Yes)
         {
             var row      = dgvPhone.Rows[dgvPhone.CurrentCell.RowIndex];
             var products = dataContext.products;
             var phone    = products.FirstOrDefault(p => p.id == int.Parse(row.Cells[0].Value.ToString()));
             products.DeleteOnSubmit(phone);
             dataContext.SubmitChanges();
             MessageBox.Show("Xóa thành công!" + phone.name);
         }
     }
     frmMain_Load(sender, e);
 }
Exemple #2
0
 private void btnSave_Click(object sender, EventArgs e)
 {
     dataContext = new ManagerPhoneDataContext();
     if (String.IsNullOrWhiteSpace(txtCode.Text) ||
         String.IsNullOrWhiteSpace(txtDescription.Text) ||
         String.IsNullOrWhiteSpace(txtName.Text))
     {
         MessageBox.Show("Field is not null.");
         return;
     }
     if (btnUpdate.Enabled == false)
     {
         var row      = dgvPhone.Rows[dgvPhone.CurrentCell.RowIndex];
         var products = dataContext.products;
         var phone    = products.FirstOrDefault(p => p.id == int.Parse(row.Cells[0].Value.ToString()));
         phone.name        = String.IsNullOrWhiteSpace(txtName.Text) ? "Chưa có tên" : txtName.Text;
         phone.description = String.IsNullOrWhiteSpace(txtDescription.Text) ? "Chưa có mô tả" : txtDescription.Text;
         if (picImage.Image == null)
         {
             var          img    = QLSanPham.Properties.Resources.open;
             MemoryStream stream = new MemoryStream();
             img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
             phone.image = stream.ToArray();
         }
         else
         {
             MemoryStream stream = new MemoryStream();
             picImage.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
             phone.image = stream.ToArray();
         }
         dataContext.SubmitChanges();
         MessageBox.Show("Cập nhật thành công!");
         btnAdd.Enabled    = true;
         btnSave.Enabled   = false;
         btnUpdate.Enabled = true;
         btnDelete.Text    = "Xóa";
         btnDelete.Enabled = true;
         frmMain_Load(sender, e);
     }
     else
     {
         var phone = new product();
         phone.code        = txtCode.Text;
         phone.description = txtDescription.Text;
         phone.name        = txtName.Text;
         MemoryStream stream = new MemoryStream();
         picImage.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
         phone.image          = stream.ToArray();
         phone.id             = 0;
         phone.code_trademark = trademark;
         var products = dataContext.products;
         try
         {
             products.InsertOnSubmit(phone);
             dataContext.SubmitChanges();
             MessageBox.Show("Thêm thành công!");
             btnAdd.Enabled    = true;
             btnSave.Enabled   = false;
             btnUpdate.Enabled = true;
             btnDelete.Text    = "Xóa";
             frmMain_Load(sender, e);
         }
         catch (Exception ex)
         {
             MessageBox.Show("Thêm thất bại." + ex.Message);
             return;
         }
     }
 }