private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            try
            {
                GridView view = sender as GridView;

                //validation
                GridColumn ma_tkCol = view.Columns["MA_TK"];
                GridColumn ma_dtpnCol = view.Columns["MA_DTPN"];
                string ma_tk = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, ma_tkCol), "");
                string ma_dtpn = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, ma_dtpnCol), "");
                if (ma_tk.Trim().Length == 0 || ma_dtpn.Trim().Length == 0)
                {
                    e.Valid = false;
                    if (ma_tk.Trim().Length == 0)
                        view.SetColumnError(ma_tkCol, "Mã TK 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;
                }

                _KTCNHoaDonRepo = new KTCNHoaDonRepo();
                //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_CNHoaDon obj = new KT_CNHoaDon();

                    obj.MA_TK = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_TK"), "");
                    obj.MA_DTPN = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_DTPN"), "");
                    obj.TEN_DTPN = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_DTPN"), "");
                    obj.SO_HOADON = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "SO_HOADON"), "");
                    DateTime? temp = null;
                    if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_HOADON"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_HOADON"), DateTime.MinValue);
                    obj.NGAY_HOADON = temp;
                    obj.SR_HOADON = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "SR_HOADON"), "");
                    obj.SO_HD = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "SO_HD"), "");
                    obj.HAN_MUC = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "HAN_MUC"), 0);
                    obj.LAI_SUAT = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "LAI_SUAT"), "");
                    obj.GIA_TRI = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "GIA_TRI"), 0);
                    obj.DUNO_VND = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "DUNO_VND"), 0);
                    obj.DUCO_VND = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "DUCO_VND"), 0);
                    obj.PSNO_VND = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "PSNO_VND"), 0);
                    obj.PSCO_VND = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "PSCO_VND"), 0);
                    obj.CKNO_VND = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "CKNO_VND"), 0);
                    obj.CKCO_VND = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "CKCO_VND"), 0);
                    obj.DUNO_USD = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "DUNO_USD"), 0);
                    obj.DUCO_USD = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "DUCO_USD"), 0);
                    obj.PSNO_USD = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "PSNO_USD"), 0);
                    obj.PSCO_USD = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "PSCO_USD"), 0);
                    obj.CKNO_USD = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "CKNO_USD"), 0);
                    obj.CKCO_USD = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "CKCO_USD"), 0);
                    obj.TUOI_NO = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "TUOI_NO"), 0);
                    obj.NO_30_VND = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NO_30_VND"), 0);
                    obj.NO_3060_VND = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NO_3060_VND"), 0);
                    obj.NO_6090_VND = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NO_6090_VND"), 0);
                    obj.NO_90_VND = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NO_90_VND"), 0);
                    obj.NO_30_USD = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NO_30_USD"), 0);
                    obj.NO_3060_USD = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NO_3060_USD"), 0);
                    obj.NO_6090_USD = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NO_6090_USD"), 0);
                    obj.NO_90_USD = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NO_90_USD"), 0);
                    temp = null;
                    if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_DH"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_DH"), DateTime.MinValue);
                    obj.NGAY_DH = temp;
                    obj.TY_GIA_NO = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "TY_GIA_NO"), 0);
                    obj.TY_GIA_CO = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "TY_GIA_CO"), 0);
                    obj.DANH_DAU = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DANH_DAU"), "");
                    obj.TRANG_THAI = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "TRANG_THAI"), 0);

                    _KTCNHoaDonRepo.Create(obj);

                }
                //Cũ thì update
                else
                {
                    int id = Utils.CIntDef(view.GetRowCellValue(gridView1.FocusedRowHandle, "ID"), 0);
                    KT_CNHoaDon obj = _KTCNHoaDonRepo.GetById(id);
                    if (obj != null)
                    {
                        obj.MA_TK = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_TK"), "");
                        obj.MA_DTPN = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_DTPN"), "");
                        obj.TEN_DTPN = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_DTPN"), "");
                        obj.SO_HOADON = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "SO_HOADON"), "");
                        DateTime? temp = null;
                        if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_HOADON"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_HOADON"), DateTime.MinValue);
                        obj.NGAY_HOADON = temp;
                        obj.SR_HOADON = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "SR_HOADON"), "");
                        obj.SO_HD = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "SO_HD"), "");
                        obj.HAN_MUC = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "HAN_MUC"), 0);
                        obj.LAI_SUAT = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "LAI_SUAT"), "");
                        obj.GIA_TRI = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "GIA_TRI"), 0);
                        obj.DUNO_VND = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "DUNO_VND"), 0);
                        obj.DUCO_VND = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "DUCO_VND"), 0);
                        obj.PSNO_VND = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "PSNO_VND"), 0);
                        obj.PSCO_VND = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "PSCO_VND"), 0);
                        obj.CKNO_VND = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "CKNO_VND"), 0);
                        obj.CKCO_VND = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "CKCO_VND"), 0);
                        obj.DUNO_USD = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "DUNO_USD"), 0);
                        obj.DUCO_USD = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "DUCO_USD"), 0);
                        obj.PSNO_USD = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "PSNO_USD"), 0);
                        obj.PSCO_USD = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "PSCO_USD"), 0);
                        obj.CKNO_USD = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "CKNO_USD"), 0);
                        obj.CKCO_USD = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "CKCO_USD"), 0);
                        obj.TUOI_NO = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "TUOI_NO"), 0);
                        obj.NO_30_VND = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NO_30_VND"), 0);
                        obj.NO_3060_VND = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NO_3060_VND"), 0);
                        obj.NO_6090_VND = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NO_6090_VND"), 0);
                        obj.NO_90_VND = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NO_90_VND"), 0);
                        obj.NO_30_USD = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NO_30_USD"), 0);
                        obj.NO_3060_USD = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NO_3060_USD"), 0);
                        obj.NO_6090_USD = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NO_6090_USD"), 0);
                        obj.NO_90_USD = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NO_90_USD"), 0);
                        temp = null;
                        if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_DH"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_DH"), DateTime.MinValue);
                        obj.NGAY_DH = temp;
                        obj.TY_GIA_NO = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "TY_GIA_NO"), 0);
                        obj.TY_GIA_CO = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "TY_GIA_CO"), 0);
                        obj.DANH_DAU = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DANH_DAU"), "");
                        obj.TRANG_THAI = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "TRANG_THAI"), 0);

                        _KTCNHoaDonRepo.Update(obj);
                    }

                }
                Load_Data();
            }
            catch (Exception ex)
            {
                e.Valid = false;
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void Save_Data(bool msg)
        {
            try
            {
                _KTCNHoaDonRepo = new KTCNHoaDonRepo();
                int i = 0;
                foreach (int pos in _listUpdate)
                {
                    int id = Utils.CIntDef(gridView1.GetRowCellValue(pos, "ID"), 0);
                    KT_CNHoaDon obj = _KTCNHoaDonRepo.GetById(id);
                    if (obj != null)
                    {
                        obj.MA_TK = Utils.CStrDef(gridView1.GetRowCellValue(pos, "MA_TK"), "");
                        obj.MA_DTPN = Utils.CStrDef(gridView1.GetRowCellValue(pos, "MA_DTPN"), "");
                        obj.TEN_DTPN = Utils.CStrDef(gridView1.GetRowCellValue(pos, "TEN_DTPN"), "");
                        obj.SO_HOADON = Utils.CStrDef(gridView1.GetRowCellValue(pos, "SO_HOADON"), "");
                        DateTime? temp = null;
                        if (Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_HOADON"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_HOADON"), DateTime.MinValue);
                        obj.NGAY_HOADON = temp;
                        obj.SR_HOADON = Utils.CStrDef(gridView1.GetRowCellValue(pos, "SR_HOADON"), "");
                        obj.SO_HD = Utils.CStrDef(gridView1.GetRowCellValue(pos, "SO_HD"), "");
                        obj.HAN_MUC = Utils.CDblDef(gridView1.GetRowCellValue(pos, "HAN_MUC"), 0);
                        obj.LAI_SUAT = Utils.CStrDef(gridView1.GetRowCellValue(pos, "LAI_SUAT"), "");
                        obj.GIA_TRI = Utils.CDblDef(gridView1.GetRowCellValue(pos, "GIA_TRI"), 0);
                        obj.DUNO_VND = Utils.CIntDef(gridView1.GetRowCellValue(pos, "DUNO_VND"), 0);
                        obj.DUCO_VND = Utils.CIntDef(gridView1.GetRowCellValue(pos, "DUCO_VND"), 0);
                        obj.PSNO_VND = Utils.CIntDef(gridView1.GetRowCellValue(pos, "PSNO_VND"), 0);
                        obj.PSCO_VND = Utils.CIntDef(gridView1.GetRowCellValue(pos, "PSCO_VND"), 0);
                        obj.CKNO_VND = Utils.CIntDef(gridView1.GetRowCellValue(pos, "CKNO_VND"), 0);
                        obj.CKCO_VND = Utils.CIntDef(gridView1.GetRowCellValue(pos, "CKCO_VND"), 0);
                        obj.DUNO_USD = Utils.CIntDef(gridView1.GetRowCellValue(pos, "DUNO_USD"), 0);
                        obj.DUCO_USD = Utils.CIntDef(gridView1.GetRowCellValue(pos, "DUCO_USD"), 0);
                        obj.PSNO_USD = Utils.CIntDef(gridView1.GetRowCellValue(pos, "PSNO_USD"), 0);
                        obj.PSCO_USD = Utils.CIntDef(gridView1.GetRowCellValue(pos, "PSCO_USD"), 0);
                        obj.CKNO_USD = Utils.CIntDef(gridView1.GetRowCellValue(pos, "CKNO_USD"), 0);
                        obj.CKCO_USD = Utils.CIntDef(gridView1.GetRowCellValue(pos, "CKCO_USD"), 0);
                        obj.TUOI_NO = Utils.CIntDef(gridView1.GetRowCellValue(pos, "TUOI_NO"), 0);
                        obj.NO_30_VND = Utils.CDblDef(gridView1.GetRowCellValue(pos, "NO_30_VND"), 0);
                        obj.NO_3060_VND = Utils.CDblDef(gridView1.GetRowCellValue(pos, "NO_3060_VND"), 0);
                        obj.NO_6090_VND = Utils.CDblDef(gridView1.GetRowCellValue(pos, "NO_6090_VND"), 0);
                        obj.NO_90_VND = Utils.CDblDef(gridView1.GetRowCellValue(pos, "NO_90_VND"), 0);
                        obj.NO_30_USD = Utils.CDblDef(gridView1.GetRowCellValue(pos, "NO_30_USD"), 0);
                        obj.NO_3060_USD = Utils.CDblDef(gridView1.GetRowCellValue(pos, "NO_3060_USD"), 0);
                        obj.NO_6090_USD = Utils.CDblDef(gridView1.GetRowCellValue(pos, "NO_6090_USD"), 0);
                        obj.NO_90_USD = Utils.CDblDef(gridView1.GetRowCellValue(pos, "NO_90_USD"), 0);
                        temp = null;
                        if (Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_DH"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_DH"), DateTime.MinValue);
                        obj.NGAY_DH = temp;
                        obj.TY_GIA_NO = Utils.CDblDef(gridView1.GetRowCellValue(pos, "TY_GIA_NO"), 0);
                        obj.TY_GIA_CO = Utils.CDblDef(gridView1.GetRowCellValue(pos, "TY_GIA_CO"), 0);
                        obj.DANH_DAU = Utils.CStrDef(gridView1.GetRowCellValue(pos, "DANH_DAU"), "");
                        obj.TRANG_THAI = Utils.CIntDef(gridView1.GetRowCellValue(pos, "TRANG_THAI"), 0);

                        _KTCNHoaDonRepo.Update(obj);
                        i++;
                    }
                }
                _listUpdate = new List<int>();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 private void Save_Tick()
 {
     try
     {
         _KTCNHoaDonRepo = new KTCNHoaDonRepo();
         int id = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID").ToString(), 0);
         KT_CNHoaDon obj = _KTCNHoaDonRepo.GetById(id);
         if (obj != null)
         {
             obj.DANH_DAU = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DANH_DAU"), "").Trim() == "T" ? "" : "T";
             _KTCNHoaDonRepo.Update(obj);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }