//Các phương thức khởi tạo của Class
        #region "Constructors"
        public frm_danhmuc_loaithuoc()
        {
            InitializeComponent();

            m_Query = DmucLoaithuoc.CreateQuery();
            InitEvents();
        }
        /// <summary>
        /// Thực hiện nghiệp vụ Delete dữ liệu
        /// </summary>
        private void PerformDeleteAction()
        {
            if (Utility.AcceptQuestion("Bạn có muốn xóa Loại thuốc đang chọn hay không?", "Xác nhận xóa", true))
            {
                Int16 v_shtIdLoaithuoc = Convert.ToInt16(txtID.Text.Trim());
                //Kiểm tra xem đã được sử dụng trong bảng khác chưa

                if (new DmucThuocController().FetchByQuery(DmucThuoc.CreateQuery().AddWhere("Id_Loaithuoc", Comparison.Equals, v_shtIdLoaithuoc)).Count > 0)
                {
                    Utility.SetMsg(lblMsg, "Loại thuốc này đã được sử dụng trong danh mục thuốc(vật tư) nên bạn không thể xóa", true);
                    return;
                }
                DataRow[] arrDr = m_dtLoaithuoc.Select(DmucLoaithuoc.Columns.IdLoaithuoc + "=" + txtID.Text);
                //Gọi nghiệp vụ xóa dữ liệu\
                int Count = DmucLoaithuoc.Delete(v_shtIdLoaithuoc);

                if (arrDr.Length > 0)//Nếu xóa thành công trong CSDL
                {
                    m_dtLoaithuoc.Rows.Remove(arrDr[0]);
                    m_dtLoaithuoc.AcceptChanges();
                    //Return to the InitialStatus
                    m_enAction = action.FirstOrFinished;
                    SetControlStatus();
                    Utility.SetMsg(lblMsg, "Đã xóa Loại có mã: " + v_shtIdLoaithuoc + " ra khỏi hệ thống.", false);
                }
                else//Có lỗi xảy ra
                {
                    Utility.SetMsg(lblMsg, "Lỗi khi xóa loại thuốc", true);
                }
            }
        }
        /// <summary>
        /// GetObject after Double click and Keydown (Enter) events of GridView
        /// </summary>
        private void GetObject()
        {
            DataRow[] arrDr = m_dtLoaithuoc.Select(DmucLoaithuoc.Columns.IdLoaithuoc + "=" + txtID.Text);

            if (arrDr.Length > 0)
            {
                m_objObjectReturn = DmucLoaithuoc.FetchByID(Convert.ToInt32(txtID.Text));
                m_blnCancel       = false;
                this.Close();
            }
            else
            {
                Utility.SetMsg(lblMsg, "Bạn hãy chọn một dòng dữ liệu trên lưới và thực hiện lại thao tác", true);
            }
        }
        /// <summary>
        /// Thực hiện nghiệp vụ Insert dữ liệu
        /// </summary>
        private void PerformInsertAction()
        {
            Utility.SetMsg(lblMsg, "", true);
            //Kiểm tra trùng tên đối tượng và cảnh báo
            DmucLoaithuocCollection v_arrSameObject = new DmucLoaithuocController().FetchByQuery(m_Query.AddWhere("ma_loaithuoc", txtDrug_Code.Text.Trim().ToUpper()));
            if (v_arrSameObject.Count > 0)
            {
                if (!Utility.AcceptQuestion("Đã có nhóm thuốc có mã:" + txtDrug_Code.Text.Trim() + ". Bạn có muốn tiếp tục ghi hay không?", "Cảnh báo", true))
                {
                    //Create Again to ignore Where Clause
                    m_Query = DmucLoaithuoc.CreateQuery();
                    txtDrug_Code.Focus();
                    return;
                }
            }
            v_arrSameObject = new DmucLoaithuocController().FetchByQuery(m_Query.AddWhere("ten_loaithuoc", txtName.Text.Trim().ToUpper()));
            if (v_arrSameObject.Count > 0)
            {
                if (!Utility.AcceptQuestion("Đã có nhóm"+tenloai+"có tên:" + txtName.Text.Trim() + ". Bạn có muốn tiếp tục ghi hay không?", "Cảnh báo", true))
                {
                    //Create Again to ignore Where Clause
                    m_Query = DmucLoaithuoc.CreateQuery();
                    txtName.Focus();
                    return;
                }
            }
            //Create Again to ignore Where Clause
            m_Query = DmucLoaithuoc.CreateQuery();
            //Gọi nghiệp vụ Insert dữ liệu

            DmucLoaithuoc objDrugType=new DmucLoaithuoc();
            objDrugType.MaLoaithuoc = Utility.sDbnull(txtDrug_Code.Text);
            objDrugType.TenLoaithuoc = Utility.sDbnull(txtName.Text);
            objDrugType.MotaThem = Utility.sDbnull(txtDesc.Text);
            objDrugType.MaNhomthuoc= Utility.sDbnull(cboNhom.SelectedValue, "");
            objDrugType.SttHthi = Convert.ToInt16(txtPos.Text);
            objDrugType.InRieng = Convert.ToInt16(chkInrieng.Checked ? 1 : 0);
            objDrugType.KieuThuocvattu = Kieuthuoc_vattu;
            objDrugType.IsNew = true;
            objDrugType.Save();
            //Lấy về MaxID vừa được thêm vào CSDL
            int v_shtIdLoaithuoc = objDrugType.IdLoaithuoc;
            //Lấy về Object vừa tạo
            DmucLoaithuocCollection v_arrNewObject = new DmucLoaithuocController().FetchByID(v_shtIdLoaithuoc);
            if (v_arrNewObject.Count > 0)//-->Thêm mới thành công
            {
                DataRow newitem=m_dtLoaithuoc.NewRow();
                Utility.FromObjectToDatarow(v_arrNewObject[0], ref newitem);
                newitem["ten_nhomthuoc"] = cboNhom.Text;
                m_dtLoaithuoc.Rows.Add(newitem);
                //Return to the InitialStatus
                m_enAction = action.FirstOrFinished;
                //Nhảy đến bản ghi vừa thêm mới trên lưới. Do txtID chưa bị reset nên dùng luôn
                Utility.GotoNewRowJanus(grdList, "Id_Loaithuoc", v_shtIdLoaithuoc.ToString());
                Utility.SetMsg(lblMsg, "Thêm mới dữ liệu thành công!",false);
                SetControlStatus();
                this.Activate();
            }
            else//Có lỗi xảy ra
                Utility.SetMsg(lblMsg, "Thêm mới không thành công. Mời bạn xem lại",false);
        }
        /// <summary>
        /// GetObject after Double click and Keydown (Enter) events of GridView
        /// </summary>
        private void GetObject()
        {
            DataRow[] arrDr = m_dtLoaithuoc.Select(DmucLoaithuoc.Columns.IdLoaithuoc + "=" + txtID.Text);

            if (arrDr.Length>0)
            {
                m_objObjectReturn = DmucLoaithuoc.FetchByID(Convert.ToInt32(txtID.Text));
                m_blnCancel = false;
                this.Close();

            }
            else
                Utility.SetMsg(lblMsg, "Bạn hãy chọn một dòng dữ liệu trên lưới và thực hiện lại thao tác",true);
        }
        public void Insert(string MaLoaithuoc,string TenLoaithuoc,short SttHthi,string MotaThem,string MaNhomthuoc,short? InRieng,string KieuThuocvattu)
        {
            DmucLoaithuoc item = new DmucLoaithuoc();

            item.MaLoaithuoc = MaLoaithuoc;

            item.TenLoaithuoc = TenLoaithuoc;

            item.SttHthi = SttHthi;

            item.MotaThem = MotaThem;

            item.MaNhomthuoc = MaNhomthuoc;

            item.InRieng = InRieng;

            item.KieuThuocvattu = KieuThuocvattu;

            item.Save(UserName);
        }
        public void Update(short IdLoaithuoc,string MaLoaithuoc,string TenLoaithuoc,short SttHthi,string MotaThem,string MaNhomthuoc,short? InRieng,string KieuThuocvattu)
        {
            DmucLoaithuoc item = new DmucLoaithuoc();
            item.MarkOld();
            item.IsLoaded = true;

            item.IdLoaithuoc = IdLoaithuoc;

            item.MaLoaithuoc = MaLoaithuoc;

            item.TenLoaithuoc = TenLoaithuoc;

            item.SttHthi = SttHthi;

            item.MotaThem = MotaThem;

            item.MaNhomthuoc = MaNhomthuoc;

            item.InRieng = InRieng;

            item.KieuThuocvattu = KieuThuocvattu;

            item.Save(UserName);
        }
        /// <summary>
        /// Thực hiện nghiệp vụ Update dữ liệu
        /// </summary>
        private void PerformUpdateAction()
        {
            Utility.SetMsg(lblMsg, "", true);

            //Gọi Business cập nhật dữ liệu
            int v_shtIdLoaithuoc = Convert.ToInt32(txtID.Text);
            //Kiểm tra trùng tên đối tượng và cảnh báo
            DmucLoaithuocCollection v_arrSameObject = new DmucLoaithuocController().FetchByQuery(m_Query.AddWhere("ma_loaithuoc", txtDrug_Code.Text.Trim().ToUpper()).AND("Id_Loaithuoc", Comparison.NotEquals, v_shtIdLoaithuoc));

            if (v_arrSameObject.Count > 0)
            {
                if (!Utility.AcceptQuestion("Đã có nhóm có mã:" + txtDrug_Code.Text.Trim() + ". Bạn có muốn tiếp tục ghi hay không?", "Cảnh báo", true))
                {
                    //Create Again to ignore Where Clause
                    m_Query = DmucDoituongkcb.CreateQuery();
                    return;
                }
            }
            v_arrSameObject = new DmucLoaithuocController().FetchByQuery(m_Query.AddWhere("ten_loaithuoc", txtName.Text.Trim().ToUpper()).AND("Id_Loaithuoc", Comparison.NotEquals, v_shtIdLoaithuoc));
            if (v_arrSameObject.Count > 0)
            {
                if (!Utility.AcceptQuestion("Đã có nhóm có tên:" + txtName.Text.Trim() + ". Bạn có muốn tiếp tục ghi hay không?", "Cảnh báo", true))
                {
                    //Create Again to ignore Where Clause
                    m_Query = DmucDoituongkcb.CreateQuery();
                    return;
                }
            }
            //Create Again to ignore Where Clause
            m_Query = DmucLoaithuoc.CreateQuery();
            DmucLoaithuoc v_NewObjectChangePos = null;

            DmucLoaithuoc objDrugType = DmucLoaithuoc.FetchByID(Convert.ToInt16(txtID.Text));

            if (objDrugType != null)
            {
                objDrugType.MaLoaithuoc    = Utility.sDbnull(txtDrug_Code.Text);
                objDrugType.TenLoaithuoc   = Utility.sDbnull(txtName.Text);
                objDrugType.MotaThem       = Utility.sDbnull(txtDesc.Text);
                objDrugType.MaNhomthuoc    = Utility.sDbnull(txtNhom.myCode, "");
                objDrugType.SttHthi        = Convert.ToInt16(txtPos.Text);
                objDrugType.InRieng        = Convert.ToInt16(chkInrieng.Checked ? 1 : 0);
                objDrugType.KieuThuocvattu = txtKieuthuocVT.myCode;
                objDrugType.IsNew          = false;
                objDrugType.MarkOld();
                objDrugType.Save();
            }
            DataRow[] arrDr = m_dtLoaithuoc.Select(DmucLoaithuoc.Columns.IdLoaithuoc + "=" + txtID.Text);
            if (arrDr.Length > 0)
            {
                arrDr[0][DmucLoaithuoc.Columns.MaLoaithuoc]    = Utility.sDbnull(txtDrug_Code.Text);
                arrDr[0][DmucLoaithuoc.Columns.TenLoaithuoc]   = Utility.sDbnull(txtName.Text);
                arrDr[0][DmucLoaithuoc.Columns.MotaThem]       = Utility.sDbnull(txtDesc.Text);
                arrDr[0][DmucLoaithuoc.Columns.MaNhomthuoc]    = Utility.sDbnull(txtNhom.myCode, "");
                arrDr[0][DmucLoaithuoc.Columns.SttHthi]        = Convert.ToInt16(txtPos.Text);
                arrDr[0][DmucLoaithuoc.Columns.InRieng]        = Convert.ToInt16(chkInrieng.Checked ? 1 : 0);
                arrDr[0][DmucLoaithuoc.Columns.KieuThuocvattu] = txtKieuthuocVT.myCode;
                arrDr[0]["ten_nhomthuoc"] = txtNhom.Text;
            }
            //Return to the InitialStatus
            m_enAction = action.FirstOrFinished;
            //Nhảy đến bản ghi vừa cập nhật trên lưới. Do txtID chưa bị reset nên dùng luôn
            Utility.GotoNewRowJanus(grdList, "Id_Loaithuoc", txtID.Text.Trim());
            SetControlStatus();
            Utility.SetMsg(lblMsg, "Cập nhật dữ liệu thành công.", false);
        }
        /// <summary>
        /// Thực hiện nghiệp vụ Insert dữ liệu
        /// </summary>
        private void PerformInsertAction()
        {
            Utility.SetMsg(lblMsg, "", true);
            //Kiểm tra trùng tên đối tượng và cảnh báo
            DmucLoaithuocCollection v_arrSameObject = new DmucLoaithuocController().FetchByQuery(m_Query.AddWhere("ma_loaithuoc", txtDrug_Code.Text.Trim().ToUpper()));

            if (v_arrSameObject.Count > 0)
            {
                if (!Utility.AcceptQuestion("Đã có nhóm thuốc có mã:" + txtDrug_Code.Text.Trim() + ". Bạn có muốn tiếp tục ghi hay không?", "Cảnh báo", true))
                {
                    //Create Again to ignore Where Clause
                    m_Query = DmucLoaithuoc.CreateQuery();
                    txtDrug_Code.Focus();
                    return;
                }
            }
            v_arrSameObject = new DmucLoaithuocController().FetchByQuery(m_Query.AddWhere("ten_loaithuoc", txtName.Text.Trim().ToUpper()));
            if (v_arrSameObject.Count > 0)
            {
                if (!Utility.AcceptQuestion("Đã có nhóm có tên:" + txtName.Text.Trim() + ". Bạn có muốn tiếp tục ghi hay không?", "Cảnh báo", true))
                {
                    //Create Again to ignore Where Clause
                    m_Query = DmucLoaithuoc.CreateQuery();
                    txtName.Focus();
                    return;
                }
            }
            //Create Again to ignore Where Clause
            m_Query = DmucLoaithuoc.CreateQuery();
            //Gọi nghiệp vụ Insert dữ liệu

            DmucLoaithuoc objDrugType = new DmucLoaithuoc();

            objDrugType.MaLoaithuoc    = Utility.sDbnull(txtDrug_Code.Text);
            objDrugType.TenLoaithuoc   = Utility.sDbnull(txtName.Text);
            objDrugType.MotaThem       = Utility.sDbnull(txtDesc.Text);
            objDrugType.MaNhomthuoc    = Utility.sDbnull(txtNhom.myCode, "");
            objDrugType.SttHthi        = Convert.ToInt16(txtPos.Text);
            objDrugType.InRieng        = Convert.ToInt16(chkInrieng.Checked ? 1 : 0);
            objDrugType.KieuThuocvattu = txtNhom.myCode;;
            objDrugType.IsNew          = true;
            objDrugType.Save();
            //Lấy về MaxID vừa được thêm vào CSDL
            int v_shtIdLoaithuoc = objDrugType.IdLoaithuoc;
            //Lấy về Object vừa tạo
            DmucLoaithuocCollection v_arrNewObject = new DmucLoaithuocController().FetchByID(v_shtIdLoaithuoc);

            if (v_arrNewObject.Count > 0)//-->Thêm mới thành công
            {
                DataRow newitem = m_dtLoaithuoc.NewRow();
                Utility.FromObjectToDatarow(v_arrNewObject[0], ref newitem);
                newitem["ten_nhomthuoc"] = txtNhom.Text;
                m_dtLoaithuoc.Rows.Add(newitem);
                //Return to the InitialStatus
                m_enAction = action.FirstOrFinished;
                //Nhảy đến bản ghi vừa thêm mới trên lưới. Do txtID chưa bị reset nên dùng luôn
                Utility.GotoNewRowJanus(grdList, "Id_Loaithuoc", v_shtIdLoaithuoc.ToString());
                Utility.SetMsg(lblMsg, "Thêm mới dữ liệu thành công!", false);
                SetControlStatus();
                this.Activate();
            }
            else//Có lỗi xảy ra
            {
                Utility.SetMsg(lblMsg, "Thêm mới không thành công. Mời bạn xem lại", false);
            }
        }
        /// <summary>
        /// Thiết lập trạng thái các Control trên Form theo thao tác nghiệp vụ cần thực hiện
        /// Insert, Update hoặc Delete...
        /// </summary>
        private void SetControlStatus()
        {
            switch (m_enAction)
            {
            case action.Insert:
                //Cho phép nhập liệu mã chủng loại thuốc,vị trí, tên chủng loại thuốc và mô tả thêm
                Utility.DisabledTextBox(txtID);
                Utility.EnabledTextBox(txtPos);
                Utility.EnabledTextBox(txtName);
                Utility.EnabledTextBox(txtDesc);
                txtNhom.Enabled        = true;
                txtKieuthuocVT.Enabled = true;
                Utility.EnabledTextBox(txtDrug_Code);
                txtDrug_Code.Clear();
                txtPos.Clear();
                txtName.Clear();
                txtDesc.Clear();
                Int16 MaxPos = Utility.Int16Dbnull(DmucLoaithuoc.CreateQuery().GetMax("stt_hthi"), 0);
                MaxPos     += 1;
                txtPos.Text = MaxPos.ToString();
                m_shtOldPos = Convert.ToInt16(txtPos.Text);
                //--------------------------------------------------------------
                //Thiết lập trạng thái các nút Insert, Update, Delete...
                //Không cho phép nhấn Insert, Update,Delete
                cmdInsert.Enabled = false;
                cmdUpdate.Enabled = false;
                cmdDelete.Enabled = false;
                //Cho phép nhấn nút Ghi
                cmdSave.Enabled = true;
                //Nút thoát biến thành nút hủy
                cmdClose.Text = "Hủy";
                //--------------------------------------------------------------
                //Không cho phép chọn trên lưới dữ liệu được fill vào các Control
                m_blnAllowCurrentCellChangedOnGridView = false;
                //Tự động Focus đến mục ID để người dùng nhập liệu
                txtID.Text = "Tự sinh";
                txtDrug_Code.Focus();
                break;

            case action.Update:
                //Không cho phép cập nhật lại mã chủng loại thuốc
                Utility.DisabledTextBox(txtID);
                //Cho phép cập nhật lại vị trí, tên chủng loại thuốc và mô tả thêm
                Utility.EnabledTextBox(txtName);
                txtNhom.Enabled        = true;
                txtKieuthuocVT.Enabled = true;
                Utility.EnabledTextBox(txtDesc);
                Utility.EnabledTextBox(txtPos);
                Utility.EnabledTextBox(txtDrug_Code);

                m_shtOldPos = Utility.Int16Dbnull(Utility.GetValueFromGridColumn(grdList, "stt_hthi"), 0);
                //--------------------------------------------------------------
                //Thiết lập trạng thái các nút Insert, Update, Delete...
                //Không cho phép nhấn Insert, Update,Delete
                cmdInsert.Enabled = false;
                cmdUpdate.Enabled = false;
                cmdDelete.Enabled = false;
                //Cho phép nhấn nút Ghi
                cmdSave.Enabled = true;
                //Nút thoát biến thành nút hủy
                cmdClose.Text = "Hủy";
                //--------------------------------------------------------------
                //Không cho phép chọn trên lưới dữ liệu được fill vào các Control
                m_blnAllowCurrentCellChangedOnGridView = false;
                //Tự động Focus đến mục Position để người dùng nhập liệu
                txtDrug_Code.Focus();
                break;

            case action.FirstOrFinished:    //Hủy hoặc trạng thái ban đầu khi mới hiển thị Form
                //Không cho phép nhập liệu mã chủng loại thuốc, tên chủng loại thuốc và mô tả thêm
                Utility.DisabledTextBox(txtID);
                Utility.DisabledTextBox(txtName);
                Utility.DisabledTextBox(txtDesc);
                txtNhom.Enabled        = false;
                txtKieuthuocVT.Enabled = false;
                Utility.DisabledTextBox(txtPos);
                Utility.DisabledTextBox(txtDrug_Code);
                //--------------------------------------------------------------
                //Thiết lập trạng thái các nút Insert, Update, Delete...
                //Sau khi nhấn Ghi thành công hoặc Hủy thao tác thì quay về trạng thái ban đầu
                //Cho phép thêm mới
                cmdInsert.Enabled = true;
                //Tùy biến nút Update và Delete tùy theo việc có hay không dữ liệu trên lưới
                cmdUpdate.Enabled = grdList.RowCount <= 0 ? false : true;
                cmdDelete.Enabled = grdList.RowCount <= 0 ? false : true;
                cmdSave.Enabled   = false;
                //Nút Hủy biến thành nút thoát
                cmdClose.Text = "Thoát";
                //--------------------------------------------------------------
                //Cho phép chọn trên lưới dữ liệu được fill vào các Control
                m_blnAllowCurrentCellChangedOnGridView = true;
                //Tự động chọn dòng hiện tại trên lưới để hiển thị lại trên Control
                grdList_CurrentCellChanged(grdList, new EventArgs());
                //Tự động Focus đến nút thêm mới?
                cmdInsert.Focus();
                break;

            default:
                break;
            }
        }