private void btnLuuDanhMuc_Click(object sender, EventArgs e) { if (txtTenDanhMuc.Text.Length <= 0) { MessageBox.Show("Yêu cầu điền thông tin danh mục!", "Thông báo", MessageBoxButtons.OK); return; } List <loai_san_pham> list = db.loai_san_pham.ToList(); foreach (loai_san_pham item in list) { if (item.ten_loai_san_pham.Equals(txtTenDanhMuc.Text)) { MessageBox.Show("Loại sản phẩm này đã tồn tại!", "Thông báo", MessageBoxButtons.OK); return; } } loai_san_pham entity = new loai_san_pham(); entity.ten_loai_san_pham = txtTenDanhMuc.Text; db.loai_san_pham.Add(entity); db.SaveChanges(); MessageBox.Show("Thêm dữ liệu thành công!", "Thông báo", MessageBoxButtons.OK); refresh(); }
private void dgvSanPham_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (this.dgvSanPham.Columns[e.ColumnIndex].Name == "danh_muc") { if (e.Value != null) { loai_san_pham category = db.loai_san_pham.Find(e.Value); e.Value = category.ten_loai_san_pham; } } if (this.dgvSanPham.Columns[e.ColumnIndex].Name == "tinh_trang") { if (e.Value != null) { e.Value = ((bool)e.Value == true) ? "Còn hàng" : "Hết hàng"; } } }
private void btnXoaDanhMuc_Click(object sender, EventArgs e) { try { if (db.san_pham.Where(x => x.loai_san_pham.ten_loai_san_pham.Equals(txtTenDanhMuc.Text)).ToList().Count > 0) { MessageBox.Show("Danh mục có sản phẩm, không thể xóa!", "Chúc mừng", MessageBoxButtons.OK); return; } if (MessageBox.Show(null, "Bạn có chắc chắn muốn xóa không?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { loai_san_pham entity = new loai_san_pham(); entity = db.loai_san_pham.SingleOrDefault(x => x.ten_loai_san_pham.Equals(txtTenDanhMuc.Text)); db.loai_san_pham.Remove(entity); db.SaveChanges(); refresh(); MessageBox.Show("Xóa dữ liệu thành công!", "Chúc mừng", MessageBoxButtons.OK); } } catch (Exception) { MessageBox.Show("Xóa thất bại", "Lỗi", MessageBoxButtons.OK); } }