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_ctCol = view.Columns["MA_CT"];
                GridColumn ma_tpCol = view.Columns["MA_TP"];
                string thang = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, thangCol), "");
                string ma_ct = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, ma_ctCol), "");
                string ma_tp = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, ma_tpCol), "");
                if (thang.Trim().Length == 0 || ma_ct.Trim().Length == 0 || ma_tp.Trim().Length == 0)
                {
                    e.Valid = false;
                    if (thang.Trim().Length == 0)
                        view.SetColumnError(thangCol, "Tháng không được rổng");
                    if (ma_ct.Trim().Length == 0)
                        view.SetColumnError(ma_ctCol, "Mã công trình không được rổng");
                    if (ma_tp.Trim().Length == 0)
                        view.SetColumnError(ma_tpCol, "Mã thành phẩm không được rổng");

                    return;
                }

                _KTGTCTRepo = new KTGTCTRepo();
                //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_GTCT obj = new KT_GTCT();

                    obj.THANG = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "THANG"), 0);
                    obj.MA_CT = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_CT"), "");
                    obj.TEN_CT = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_CT"), "");
                    obj.MA_TP = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_TP"), "");
                    obj.TEN_TP = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_TP"), "");
                    obj.SL_TP = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "SL_TP"), 0);
                    obj.DOANH_THU = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "DOANH_THU"), 0);
                    obj.DG_BAN = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "DG_BAN"), 0);
                    DateTime? temp = null;
                    if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_HD"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_HD"), DateTime.MinValue);
                    obj.NGAY_HD = temp;
                    obj.SO_HD = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "SO_HD"), "");
                    obj.NOI_DUNG = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "NOI_DUNG"), "");
                    temp = null;
                    if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_KC"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_KC"), DateTime.MinValue);
                    obj.NGAY_KC = temp;
                    obj.PSTK_621 = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "PSTK_621"), 0);
                    obj.PSTK_622 = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "PSTK_622"), 0);
                    obj.PSTK_623 = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "PSTK_623"), 0);
                    obj.PSTK_627 = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "PSTK_627"), 0);
                    obj.PSTK_TONG = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "PSTK_TONG"), 0);
                    obj.TLPB = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "TLPB"), 0);
                    obj.Z1TP = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "Z1TP"), 0);
                    obj.ZTP = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "ZTP"), 0);
                    obj.DANH_DAU = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DANH_DAU"), "");

                    _KTGTCTRepo.Create(obj);

                }
                //Cũ thì update
                else
                {
                    int id = Utils.CIntDef(view.GetRowCellValue(gridView1.FocusedRowHandle, "ID"), 0);
                    KT_GTCT obj = _KTGTCTRepo.GetById(id);
                    if (obj != null)
                    {
                        obj.THANG = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "THANG"), 0);
                        obj.MA_CT = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_CT"), "");
                        obj.TEN_CT = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_CT"), "");
                        obj.MA_TP = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_TP"), "");
                        obj.TEN_TP = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_TP"), "");
                        obj.SL_TP = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "SL_TP"), 0);
                        obj.DOANH_THU = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "DOANH_THU"), 0);
                        obj.DG_BAN = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "DG_BAN"), 0);
                        DateTime? temp = null;
                        if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_HD"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_HD"), DateTime.MinValue);
                        obj.NGAY_HD = temp;
                        obj.SO_HD = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "SO_HD"), "");
                        obj.NOI_DUNG = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "NOI_DUNG"), "");
                        temp = null;
                        if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_KC"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_KC"), DateTime.MinValue);
                        obj.NGAY_KC = temp;
                        obj.PSTK_621 = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "PSTK_621"), 0);
                        obj.PSTK_622 = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "PSTK_622"), 0);
                        obj.PSTK_623 = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "PSTK_623"), 0);
                        obj.PSTK_627 = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "PSTK_627"), 0);
                        obj.PSTK_TONG = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "PSTK_TONG"), 0);
                        obj.TLPB = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "TLPB"), 0);
                        obj.Z1TP = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "Z1TP"), 0);
                        obj.ZTP = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "ZTP"), 0);
                        obj.DANH_DAU = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DANH_DAU"), "");

                        _KTGTCTRepo.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
            {
                _KTGTCTRepo = new KTGTCTRepo();
                int i = 0;
                foreach (int pos in _listUpdate)
                {
                    int id = Utils.CIntDef(gridView1.GetRowCellValue(pos, "ID"), 0);
                    KT_GTCT obj = _KTGTCTRepo.GetById(id);
                    if (obj != null)
                    {
                        obj.THANG = Utils.CIntDef(gridView1.GetRowCellValue(pos, "THANG"), 0);
                        obj.MA_CT = Utils.CStrDef(gridView1.GetRowCellValue(pos, "MA_CT"), "");
                        obj.TEN_CT = Utils.CStrDef(gridView1.GetRowCellValue(pos, "TEN_CT"), "");
                        obj.MA_TP = Utils.CStrDef(gridView1.GetRowCellValue(pos, "MA_TP"), "");
                        obj.TEN_TP = Utils.CStrDef(gridView1.GetRowCellValue(pos, "TEN_TP"), "");
                        obj.SL_TP = Utils.CDblDef(gridView1.GetRowCellValue(pos, "SL_TP"), 0);
                        obj.DOANH_THU = Utils.CDblDef(gridView1.GetRowCellValue(pos, "DOANH_THU"), 0);
                        obj.DG_BAN = Utils.CDblDef(gridView1.GetRowCellValue(pos, "DG_BAN"), 0);
                        DateTime? temp = null;
                        if (Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_HD"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_HD"), DateTime.MinValue);
                        obj.NGAY_HD = temp;
                        obj.SO_HD = Utils.CStrDef(gridView1.GetRowCellValue(pos, "SO_HD"), "");
                        obj.NOI_DUNG = Utils.CStrDef(gridView1.GetRowCellValue(pos, "NOI_DUNG"), "");
                        temp = null;
                        if (Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_KC"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_KC"), DateTime.MinValue);
                        obj.NGAY_KC = temp;
                        obj.PSTK_621 = Utils.CDblDef(gridView1.GetRowCellValue(pos, "PSTK_621"), 0);
                        obj.PSTK_622 = Utils.CDblDef(gridView1.GetRowCellValue(pos, "PSTK_622"), 0);
                        obj.PSTK_623 = Utils.CDblDef(gridView1.GetRowCellValue(pos, "PSTK_623"), 0);
                        obj.PSTK_627 = Utils.CDblDef(gridView1.GetRowCellValue(pos, "PSTK_627"), 0);
                        obj.PSTK_TONG = Utils.CDblDef(gridView1.GetRowCellValue(pos, "PSTK_TONG"), 0);
                        obj.TLPB = Utils.CIntDef(gridView1.GetRowCellValue(pos, "TLPB"), 0);
                        obj.Z1TP = Utils.CIntDef(gridView1.GetRowCellValue(pos, "Z1TP"), 0);
                        obj.ZTP = Utils.CIntDef(gridView1.GetRowCellValue(pos, "ZTP"), 0);
                        obj.DANH_DAU = Utils.CStrDef(gridView1.GetRowCellValue(pos, "DANH_DAU"), "");

                        _KTGTCTRepo.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 Save_Tick()
 {
     try
     {
         _KTGTCTRepo = new KTGTCTRepo();
         int id = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID").ToString(), 0);
         KT_GTCT obj = _KTGTCTRepo.GetById(id);
         if (obj != null)
         {
             obj.DANH_DAU = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DANH_DAU"), "").Trim() == "T" ? "" : "T";
             _KTGTCTRepo.Update(obj);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }