private void Load_Data()
 {
     try
     {
         _KTTSCDRepo = new KTTSCDRepo();
         gridData.DataSource = _KTTSCDRepo.GetAllByThangNam(Utils.CStrDef(fTerm._month, ""), Utils.CStrDef(fTerm._year, ""));
         //_db = new dbVstoreAppDataContext(Const.builder.ConnectionString);
         //gridData.DataSource = null;
         //gridData.DataSource = _db.KT_DMHHs;
         //gridData.RefreshDataSource();
         //view.RefreshData();
     }
     catch (Exception) { }
 }
        private void Save_Data(bool msg)
        {
            try
            {
                _KTTSCDRepo = new KTTSCDRepo();
                int i = 0;
                foreach (int pos in _listUpdate)
                {
                    int id = Utils.CIntDef(gridView1.GetRowCellValue(pos, "ID"), 0);
                    KT_TSCD obj = _KTTSCDRepo.GetById(id);
                    if (obj != null)
                    {
                        obj.THANG = Utils.CStrDef(gridView1.GetRowCellValue(pos, "THANG"), "");
                        obj.NAM = Utils.CStrDef(gridView1.GetRowCellValue(pos, "NAM"), "");
                        obj.MA_TS = Utils.CStrDef(gridView1.GetRowCellValue(pos, "MA_TS"), "");
                        obj.TEN_TS = Utils.CStrDef(gridView1.GetRowCellValue(pos, "TEN_TS"), "");
                        obj.CO_KH = Utils.CStrDef(gridView1.GetRowCellValue(pos, "CO_KH"), "");
                        obj.TK_TS = Utils.CStrDef(gridView1.GetRowCellValue(pos, "TK_TS"), "");
                        obj.TK_CP = Utils.CStrDef(gridView1.GetRowCellValue(pos, "TK_CP"), "");
                        obj.TK_HM = Utils.CStrDef(gridView1.GetRowCellValue(pos, "TK_HM"), "");
                        obj.DT_SUDUNG = Utils.CStrDef(gridView1.GetRowCellValue(pos, "DT_SUDUNG"), "");

                        obj.NGUYEN_GIA = Utils.CDblDef(gridView1.GetRowCellValue(pos, "NGUYEN_GIA"), 0);
                        DateTime? temp = null;
                        if (Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_MUA"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_MUA"), DateTime.MinValue);
                        obj.NGAY_MUA = temp;
                        temp = null;
                        if (Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_BAN"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_BAN"), DateTime.MinValue);
                        obj.NGAY_BAN = temp;
                        temp = null;
                        if (Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_TRICH_KH"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_TRICH_KH"), DateTime.MinValue);
                        obj.NGAY_TRICH_KH = temp;
                        obj.THOI_GIAN_KH = Utils.CDblDef(gridView1.GetRowCellValue(pos, "THOI_GIAN_KH"), 0);
                        obj.KH_1_THANG = Utils.CDblDef(gridView1.GetRowCellValue(pos, "KH_1_THANG"), 0);
                        obj.KH_THANG_NAY = Utils.CDblDef(gridView1.GetRowCellValue(pos, "KH_THANG_NAY"), 0);
                        obj.GT_CL_DAUNAM = Utils.CDblDef(gridView1.GetRowCellValue(pos, "GT_CL_DAUNAM"), 0);
                        obj.LK_KH_DAUNAM = Utils.CDblDef(gridView1.GetRowCellValue(pos, "LK_KH_DAUNAM"), 0);
                        obj.MA_DTPN_NO = Utils.CStrDef(gridView1.GetRowCellValue(pos, "MA_DTPN_NO"), "");
                        obj.MA_DTPN_CO = Utils.CStrDef(gridView1.GetRowCellValue(pos, "MA_DTPN_CO"), "");
                        obj.MA_CT = Utils.CStrDef(gridView1.GetRowCellValue(pos, "MA_CT"), "");
                        obj.MA_YTCP_NO = Utils.CStrDef(gridView1.GetRowCellValue(pos, "MA_YTCP_NO"), "");
                        obj.DANH_DAU = Utils.CStrDef(gridView1.GetRowCellValue(pos, "DANH_DAU"), "");
                        obj.TRANG_THAI = Utils.CIntDef(gridView1.GetRowCellValue(pos, "TRANG_THAI"), 0);

                        _KTTSCDRepo.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 thangCol = view.Columns["THANG"];
                GridColumn ma_tsCol = view.Columns["MA_TS"];
                string thang = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, thangCol), "");
                string ma_ts = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, ma_tsCol), "");
                if (thang.Trim().Length == 0 || ma_ts.Trim().Length == 0)
                {
                    e.Valid = false;
                    if (thang.Trim().Length == 0)
                        view.SetColumnError(thangCol, "Tháng không được rổng");
                    if (ma_ts.Trim().Length == 0)
                        view.SetColumnError(ma_tsCol, "Mã tài sản không được rổng");

                    return;
                }

                _KTTSCDRepo = new KTTSCDRepo();
                //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_TSCD obj = new KT_TSCD();

                    obj.THANG = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "THANG"), "");
                    obj.NAM = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "NAM"), "");
                    obj.MA_TS = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_TS"), "");
                    obj.TEN_TS = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_TS"), "");
                    obj.CO_KH = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "CO_KH"), "");
                    obj.TK_TS = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TK_TS"), "");
                    obj.TK_CP = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TK_CP"), "");
                    obj.TK_HM = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TK_HM"), "");
                    obj.DT_SUDUNG = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DT_SUDUNG"), "");

                    obj.NGUYEN_GIA = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NGUYEN_GIA"), 0);
                    DateTime? temp = null;
                    if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_MUA"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_MUA"), DateTime.MinValue);
                    obj.NGAY_MUA = temp;
                    temp = null;
                    if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_BAN"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_BAN"), DateTime.MinValue);
                    obj.NGAY_BAN = temp;
                    temp = null;
                    if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_TRICH_KH"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_TRICH_KH"), DateTime.MinValue);
                    obj.NGAY_TRICH_KH = temp;
                    obj.THOI_GIAN_KH = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "THOI_GIAN_KH"), 0);
                    obj.KH_1_THANG = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "KH_1_THANG"), 0);
                    obj.KH_THANG_NAY = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "KH_THANG_NAY"), 0);
                    obj.GT_CL_DAUNAM = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "GT_CL_DAUNAM"), 0);
                    obj.LK_KH_DAUNAM = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "LK_KH_DAUNAM"), 0);
                    obj.MA_DTPN_NO = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_DTPN_NO"), "");
                    obj.MA_DTPN_CO = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_DTPN_CO"), "");
                    obj.MA_CT = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_CT"), "");
                    obj.MA_YTCP_NO = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_YTCP_NO"), "");
                    obj.DANH_DAU = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DANH_DAU"), "");
                    obj.TRANG_THAI = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "TRANG_THAI"), 0);

                    _KTTSCDRepo.Create(obj);

                }
                //Cũ thì update
                else
                {
                    int id = Utils.CIntDef(view.GetRowCellValue(view.FocusedRowHandle, "ID"), 0);
                    KT_TSCD obj = _KTTSCDRepo.GetById(id);
                    if (obj != null)
                    {
                        obj.THANG = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "THANG"), "");
                        obj.NAM = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "NAM"), "");
                        obj.MA_TS = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_TS"), "");
                        obj.TEN_TS = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_TS"), "");
                        obj.CO_KH = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "CO_KH"), "");
                        obj.TK_TS = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TK_TS"), "");
                        obj.TK_CP = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TK_CP"), "");
                        obj.TK_HM = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TK_HM"), "");
                        obj.DT_SUDUNG = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DT_SUDUNG"), "");

                        obj.NGUYEN_GIA = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NGUYEN_GIA"), 0);
                        DateTime? temp = null;
                        if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_MUA"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_MUA"), DateTime.MinValue);
                        obj.NGAY_MUA = temp;
                        temp = null;
                        if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_BAN"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_BAN"), DateTime.MinValue);
                        obj.NGAY_BAN = temp;
                        temp = null;
                        if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_TRICH_KH"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_TRICH_KH"), DateTime.MinValue);
                        obj.NGAY_TRICH_KH = temp;
                        obj.THOI_GIAN_KH = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "THOI_GIAN_KH"), 0);
                        obj.KH_1_THANG = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "KH_1_THANG"), 0);
                        obj.KH_THANG_NAY = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "KH_THANG_NAY"), 0);
                        obj.GT_CL_DAUNAM = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "GT_CL_DAUNAM"), 0);
                        obj.LK_KH_DAUNAM = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "LK_KH_DAUNAM"), 0);
                        obj.MA_DTPN_NO = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_DTPN_NO"), "");
                        obj.MA_DTPN_CO = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_DTPN_CO"), "");
                        obj.MA_CT = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_CT"), "");
                        obj.MA_YTCP_NO = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_YTCP_NO"), "");
                        obj.DANH_DAU = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DANH_DAU"), "");
                        obj.TRANG_THAI = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "TRANG_THAI"), 0);

                        _KTTSCDRepo.Update(obj);
                    }

                }
                Load_Data();
            }
            catch (Exception ex)
            {
                e.Valid = false;
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void Remove_Data()
        {
            try
            {
                //if (_listUpdate.Count > 0)
                //{
                //    MessageBox.Show("Hãy thực hiện lưu trước khi xóa!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    return;
                //}
                Save_Data(false);

                int id = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID"), 0);
                _KTTSCDRepo = new KTTSCDRepo();
                _KTTSCDRepo.Remove(id);

                //MessageBox.Show("Xóa dòng ID:" + Id + " thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Load_Data();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void Save_Duplicate()
        {
            try
            {                
                if (MessageBox.Show("Bạn có muốn copy dòng này thành dòng mới?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    _KTTSCDRepo = new KTTSCDRepo();
                    KT_TSCD obj = new KT_TSCD();

                    obj.THANG = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "THANG"), "");
                    obj.NAM = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NAM"), "");
                    obj.MA_TS = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MA_TS"), "");
                    obj.TEN_TS = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TEN_TS"), "");
                    obj.CO_KH = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "CO_KH"), "");
                    obj.TK_TS = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TK_TS"), "");
                    obj.TK_CP = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TK_CP"), "");
                    obj.TK_HM = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TK_HM"), "");
                    obj.DT_SUDUNG = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DT_SUDUNG"), "");

                    obj.NGUYEN_GIA = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGUYEN_GIA"), 0);
                    DateTime? temp = null;
                    if (Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_MUA"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_MUA"), DateTime.MinValue);
                    obj.NGAY_MUA = temp;
                    temp = null;
                    if (Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_BAN"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_BAN"), DateTime.MinValue);
                    obj.NGAY_BAN = temp;
                    temp = null;
                    if (Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_TRICH_KH"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_TRICH_KH"), DateTime.MinValue);
                    obj.NGAY_TRICH_KH = temp;
                    obj.THOI_GIAN_KH = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "THOI_GIAN_KH"), 0);
                    obj.KH_1_THANG = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "KH_1_THANG"), 0);
                    obj.KH_THANG_NAY = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "KH_THANG_NAY"), 0);
                    obj.GT_CL_DAUNAM = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "GT_CL_DAUNAM"), 0);
                    obj.LK_KH_DAUNAM = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "LK_KH_DAUNAM"), 0);
                    obj.MA_DTPN_NO = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MA_DTPN_NO"), "");
                    obj.MA_DTPN_CO = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MA_DTPN_CO"), "");
                    obj.MA_CT = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MA_CT"), "");
                    obj.MA_YTCP_NO = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MA_YTCP_NO"), "");
                    obj.DANH_DAU = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DANH_DAU"), "");
                    obj.TRANG_THAI = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TRANG_THAI"), 0);

                    _KTTSCDRepo.Create(obj);
                    MessageBox.Show("Đã copy dòng này vào cuối bảng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 private void Save_Tick()
 {
     try
     {
         _KTTSCDRepo = new KTTSCDRepo();
         int id = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID").ToString(), 0);
         KT_TSCD obj = _KTTSCDRepo.GetById(id);
         if (obj != null)
         {
             obj.DANH_DAU = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DANH_DAU"), "").Trim() == "T" ? "" : "T";
             _KTTSCDRepo.Update(obj);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }