private void Save_Data(bool msg)
        {
            try
            {
                _KTDMHHLoRepo = new KTDMHHLoRepo();
                int i = 0;
                foreach (int pos in _listUpdate)
                {
                    int id = Utils.CIntDef(gridView1.GetRowCellValue(pos, "ID"), 0);
                    KT_DMHH_Lo obj = _KTDMHHLoRepo.GetById(id);
                    if (obj != null)
                    {
                        obj.MA_DM = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MA_DM"), "");
                        obj.TEN_DM = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TEN_DM"), "");
                        obj.DANH_DAU = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DANH_DAU"), "");
                        obj.MA_TK = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MA_TK"), "");
                        obj.MA_DTPN = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MA_DTPN"), "");
                        obj.MA_LO = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MA_LO"), "");
                        DateTime? temp = null;
                        if (Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_LO"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_LO"), DateTime.MinValue);
                        obj.NGAY_LO = temp;
                        temp = null;
                        if (Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "HSD"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "HSD"), DateTime.MinValue);
                        obj.HSD = temp;
                        obj.TEN_DTPN = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TEN_DTPN"), "");
                        obj.KY_HIEU = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "KY_HIEU"), "");
                        obj.DON_VI = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DON_VI"), "");
                        obj.DG_XUAT = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DG_XUAT"), 0);
                        obj.TON_DK_SL = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TON_DK_SL"), 0);
                        obj.TON_DK_GT_VND = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TON_DK_GT_VND"), 0);
                        obj.NHAP_SL = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NHAP_SL"), 0);
                        obj.NHAP_GT_VND = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NHAP_GT_VND"), 0);
                        obj.XUAT_SL = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "XUAT_SL"), 0);
                        obj.XUAT_GT_USD = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "XUAT_GT_USD"), 0);
                        obj.TON_CK_SL = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TON_CK_SL"), 0);
                        obj.TON_CK_GT_VND = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TON_CK_GT_VND"), 0);

                        _KTDMHHLoRepo.Update(obj);
                        i++;
                    }
                }
                _listUpdate = new List<int>();
                //if (i > 0 && msg)
                //{
                //    MessageBox.Show("Lưu thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            try
            {
                GridView view = sender as GridView;

                //validation
                GridColumn ma_dmCol = view.Columns["MA_DM"];
                GridColumn ma_loCol = view.Columns["MA_LO"];
                GridColumn ma_tkCol = view.Columns["MA_TK"];
                GridColumn ma_dtpnCol = view.Columns["MA_DTPN"];
                string ma_dm = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, ma_dmCol), "");
                string ma_lo = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, ma_loCol), "");
                string ma_tk = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, ma_tkCol), "");
                string ma_dtpn = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, ma_dtpnCol), "");
                if (ma_dm.Trim().Length == 0 || ma_lo.Trim().Length == 0 || ma_tk.Trim().Length == 0 || ma_dtpn.Trim().Length == 0)
                {
                    e.Valid = false;
                    if (ma_dm.Trim().Length == 0)
                        view.SetColumnError(ma_dmCol, "Mã danh mục không được rổng");
                    if (ma_lo.Trim().Length == 0)
                        view.SetColumnError(ma_loCol, "Mã lô không được rổng");
                    if (ma_tk.Trim().Length == 0)
                        view.SetColumnError(ma_tkCol, "Mã tài khoản không được rổng");
                    if (ma_dtpn.Trim().Length == 0)
                        view.SetColumnError(ma_dtpnCol, "Mã đối tượng pháp nhân không được rổng");

                    return;
                }
                _KTDMHHLoRepo = new KTDMHHLoRepo();

                //Kiểm tra đây là dòng dữ liệu mới hay cũ, nếu là mới thì mình insert
                if (view.IsNewItemRow(e.RowHandle))
                {
                    //e.RowHandle trả về giá trị int là thứ tự của dòng hiện tại
                    KT_DMHH_Lo obj = new KT_DMHH_Lo();

                    obj.MA_DM = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_DM"), "");
                    obj.TEN_DM = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_DM"), "");
                    obj.DANH_DAU = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DANH_DAU"), "");
                    obj.MA_TK = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_TK"), "");
                    obj.MA_DTPN = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_DTPN"), "");
                    obj.MA_LO = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_LO"), "");
                    DateTime? temp = null;
                    if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_LO"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_LO"), DateTime.MinValue);
                    obj.NGAY_LO = temp;
                    temp = null;
                    if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "HSD"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "HSD"), DateTime.MinValue);
                    obj.HSD = temp;
                    obj.TEN_DTPN = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_DTPN"), "");
                    obj.KY_HIEU = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "KY_HIEU"), "");
                    obj.DON_VI = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DON_VI"), "");
                    obj.DG_XUAT = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "DG_XUAT"), 0);
                    obj.TON_DK_SL = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "TON_DK_SL"), 0);
                    obj.TON_DK_GT_VND = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "TON_DK_GT_VND"), 0);
                    obj.NHAP_SL = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NHAP_SL"), 0);
                    obj.NHAP_GT_VND = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NHAP_GT_VND"), 0);
                    obj.XUAT_SL = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "XUAT_SL"), 0);
                    obj.XUAT_GT_USD = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "XUAT_GT_USD"), 0);
                    obj.TON_CK_SL = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "TON_CK_SL"), 0);
                    obj.TON_CK_GT_VND = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "TON_CK_GT_VND"), 0);

                    _KTDMHHLoRepo.Create(obj);

                }
                //Cũ thì update
                else
                {
                    int id = Utils.CIntDef(view.GetRowCellValue(gridView1.FocusedRowHandle, "ID"), 0);
                    KT_DMHH_Lo obj = _KTDMHHLoRepo.GetById(id);
                    if (obj != null)
                    {
                        obj.MA_DM = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_DM"), "");
                        obj.TEN_DM = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_DM"), "");
                        obj.DANH_DAU = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DANH_DAU"), "");
                        obj.MA_TK = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_TK"), "");
                        obj.MA_DTPN = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_DTPN"), "");
                        obj.MA_LO = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_LO"), "");
                        DateTime? temp = null;
                        if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_LO"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_LO"), DateTime.MinValue);
                        obj.NGAY_LO = temp;
                        temp = null;
                        if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "HSD"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "HSD"), DateTime.MinValue);
                        obj.HSD = temp;
                        obj.TEN_DTPN = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_DTPN"), "");
                        obj.KY_HIEU = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "KY_HIEU"), "");
                        obj.DON_VI = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DON_VI"), "");
                        obj.DG_XUAT = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "DG_XUAT"), 0);
                        obj.TON_DK_SL = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "TON_DK_SL"), 0);
                        obj.TON_DK_GT_VND = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "TON_DK_GT_VND"), 0);
                        obj.NHAP_SL = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NHAP_SL"), 0);
                        obj.NHAP_GT_VND = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NHAP_GT_VND"), 0);
                        obj.XUAT_SL = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "XUAT_SL"), 0);
                        obj.XUAT_GT_USD = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "XUAT_GT_USD"), 0);
                        obj.TON_CK_SL = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "TON_CK_SL"), 0);
                        obj.TON_CK_GT_VND = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "TON_CK_GT_VND"), 0);

                        _KTDMHHLoRepo.Update(obj);
                    }

                }
                Load_Data();
            }
            catch (Exception ex)
            {
                e.Valid = false;
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }