private void dgvloai_CellClick(object sender, DataGridViewCellEventArgs e) { btluu.Enabled = true; txtloai.Text = dgvloai.Rows[e.RowIndex].Cells["tenloai"].Value.ToString(); txtloai.ForeColor = Color.Black; mloai = int.Parse(dgvloai.Rows[e.RowIndex].Cells["maloai"].Value.ToString()); if (e.ColumnIndex == 0) { if (MessageBox.Show(this, "Bạn có muốn xóa loại đồ uống này không ?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { using (DatabaseQLTSEntities quanli = new DatabaseQLTSEntities()) { DoUong du = quanli.DoUongs.FirstOrDefault(p => p.IdLoai == mloai); if (du != null) { MessageBox.Show(this, "Dữ liệu đang sử dụng không thể xóa !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } LoaiDoUong loai = quanli.LoaiDoUongs.FirstOrDefault(p => p.IdLoai == mloai); quanli.LoaiDoUongs.Remove(loai); quanli.SaveChanges(); loadLoaiDU(); } } } }
private void btluu_Click(object sender, EventArgs e) { if (txtloai.Text == "") { MessageBox.Show("yêu cầu nhập tên loại!"); return; } else { using (DatabaseQLTSEntities quanli = new DatabaseQLTSEntities()) { LoaiDoUong loai = quanli.LoaiDoUongs.FirstOrDefault(p => p.IdLoai == mloai); loai.TenLoai = txtloai.Text; quanli.SaveChanges(); MessageBox.Show(this, "Đã Lưu !", "Lưu", MessageBoxButtons.OK, MessageBoxIcon.Information); loadLoaiDU(); } } }