private void btnSua_Click(object sender, EventArgs e)
        {
            if (!CheckLuaChon())
            {
                return;
            }

            if (btnSua.Text == "Sửa")
            {
                btnSua.Text     = "Lưu";
                btnXoa.Text     = "Hủy";
                btnThem.Enabled = false;

                UnlockControl();

                return;
            }

            if (btnSua.Text == "Lưu")
            {
                if (Check())
                {
                    btnSua.Text = "Sửa";
                    btnXoa.Text = "Xóa";

                    LockControl();

                    KHUVUCBAN cu  = getKHUVUCBANByID();
                    KHUVUCBAN moi = getKHUVUCBANByForm();
                    CapNhat(ref cu, moi);

                    try
                    {
                        db.SaveChanges();
                        MessageBox.Show("Sưa thông tin khu vực thành công",
                                        "Thông báo",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Sửa thông tin khu vực thất bại\n" + ex.Message,
                                        "Thông báo",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                    }
                    LoadDgvKHUVUCBAN();
                }

                return;
            }
        }
        private KHUVUCBAN getKHUVUCBANByForm()
        {
            KHUVUCBAN ans = new KHUVUCBAN();

            try
            {
                ans.TEN   = txtTenKhuVuc.Text;
                ans.VITRI = txtViTri.Text;
            }
            catch { }

            return(ans);
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (btnXoa.Text == "Xóa")
            {
                if (!CheckLuaChon())
                {
                    return;
                }

                KHUVUCBAN    cu = getKHUVUCBANByID();
                DialogResult rs = MessageBox.Show("Bạn có chắc chắn xóa khu vực " + cu.TEN + "?",
                                                  "Thông báo",
                                                  MessageBoxButtons.OKCancel,
                                                  MessageBoxIcon.Warning);

                if (rs == DialogResult.Cancel)
                {
                    return;
                }

                try
                {
                    db.KHUVUCBANs.Remove(cu);
                    db.SaveChanges();
                    MessageBox.Show("Xóa thông tin khu vực thành công",
                                    "Thông báo",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Xóa thông tin khu vực thất bại\n" + ex.Message,
                                    "Thông báo",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
                LoadDgvKHUVUCBAN();

                return;
            }
            if (btnXoa.Text == "Hủy")
            {
                btnSua.Text  = "Sửa";
                btnThem.Text = "Thêm";
                btnXoa.Text  = "Xóa";

                LockControl();
                UpdateDetail();
                return;
            }
        }
        private bool CheckLuaChon()
        {
            KHUVUCBAN tg = getKHUVUCBANByID();

            if (tg.ID == 0)
            {
                MessageBox.Show("Chưa có khu vực nào được chọn",
                                "Thông báo",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return(false);
            }
            return(true);
        }
 private KHUVUCBAN getKHUVUCBANByID()
 {
     try
     {
         int       id  = (int)dgvKhuVuc.GetFocusedRowCellValue("ID");
         KHUVUCBAN ans = db.KHUVUCBANs.Where(p => p.ID == id).FirstOrDefault();
         if (ans == null)
         {
             return(new KHUVUCBAN());
         }
         return(ans);
     }
     catch
     {
         return(new KHUVUCBAN());
     }
 }
        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "Thêm")
            {
                btnSua.Enabled = false;
                btnThem.Text   = "Lưu";
                btnXoa.Text    = "Hủy";

                ClearControl();
                UnlockControl();


                return;
            }

            if (btnThem.Text == "Lưu")
            {
                if (Check())
                {
                    btnThem.Text = "Thêm";
                    btnXoa.Text  = "Xóa";
                    LockControl();

                    KHUVUCBAN moi = getKHUVUCBANByForm();
                    db.KHUVUCBANs.Add(moi);

                    try
                    {
                        db.SaveChanges();
                        MessageBox.Show("Thêm thông tin khu vực thành công",
                                        "Thông báo",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Thêm thông tin khu vực thất bại\n" + ex.Message,
                                        "Thông báo",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                    }
                    LoadDgvKHUVUCBAN();
                }
                return;
            }
        }
        private void UpdateDetail()
        {
            try
            {
                KHUVUCBAN tg = getKHUVUCBANByID();

                if (tg.ID == 0)
                {
                    return;
                }

                txtTenKhuVuc.Text = tg.TEN;
                txtViTri.Text     = tg.VITRI;
            }
            catch
            {
            }
        }
 private void CapNhat(ref KHUVUCBAN cu, KHUVUCBAN moi)
 {
     cu.TEN   = moi.TEN;
     cu.VITRI = moi.VITRI;
 }