private void btnEditProduct_Click(object sender, EventArgs e) { if (Role == 1) { int id = Convert.ToInt32(txtIdProduct.Text); Data.Product pr = new Data.Product(); pr = (from p in manage.Products where p.ProductID == id select p).FirstOrDefault(); pr.ProductName = txtNameProduct.Text; pr.QuantityInStock = Convert.ToInt32(numberQuantityProduct.Value); pr.Price = Convert.ToInt32(numberPriceProduct.Value); pr.CategoryID = Convert.ToInt32(ComboCateProduct.SelectedValue); pr.BrandID = Convert.ToInt32(ComboBrandProduct.SelectedValue); pr.Avatar = ""; manage.SaveChanges(); MessageBox.Show("Bạn đã sửa 1 mục"); LoadGridViewProduct(); } else { MessageBox.Show("Bạn không có quyền sửa !"); } }
private void btnSaveProduct_Click(object sender, EventArgs e) { string name = txtNameProduct.Text; var cate = Convert.ToInt32(ComboCateProduct.SelectedValue); int quantity = Convert.ToInt32(numberQuantityProduct.Value); int price = Convert.ToInt32(numberPriceProduct.Value); var brand = Convert.ToInt32(ComboBrandProduct.SelectedValue); Data.Product pro = new Data.Product(); pro.ProductName = name; pro.QuantityInStock = quantity; pro.CategoryID = cate; pro.BrandID = brand; pro.Price = price; pro.Avatar = ""; manage.Products.Add(pro); manage.SaveChanges(); txtNameProduct.Text = ""; numberQuantityProduct.Value = 0; numberPriceProduct.Value = 0; txtIdProduct.Text = ""; ComboBrandProduct.DataSource = (from b in manage.Brands select b).ToList(); ComboBrandProduct.ValueMember = "BrandID"; ComboBrandProduct.DisplayMember = "BrandName"; ComboCateProduct.DataSource = (from c in manage.Categories select c).ToList(); ComboCateProduct.ValueMember = "CategoryID"; ComboCateProduct.DisplayMember = "CategoryName"; ComboBrandProduct.SelectedItem = 0; ComboCateProduct.SelectedItem = 0; MessageBox.Show("Bạn đã thêm thành công"); LoadGridViewProduct(); }
private void btnRemoveProduct_Click(object sender, EventArgs e) { DialogResult dialogResult = MessageBox.Show("Bạn có muốn xóa không?", "Some Title", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { if (Role == 1) { int id = Convert.ToInt32(txtIdProduct.Text); Data.Product pr = new Data.Product(); pr = (from p in manage.Products where p.ProductID == id select p).FirstOrDefault(); manage.Entry(pr).State = System.Data.Entity.EntityState.Deleted; manage.SaveChanges(); MessageBox.Show("Bạn đã xóa thành công"); LoadGridViewProduct(); } else { MessageBox.Show("Bạn không có quyền xóa"); } } }