partial void DeleteKT_CNKU(KT_CNKU instance);
 partial void UpdateKT_CNKU(KT_CNKU instance);
        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"];
                GridColumn so_kuCol = view.Columns["SO_KU"];
                GridColumn so_hdCol = view.Columns["SO_HD"];
                string ma_tk = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, ma_tkCol), "");
                string ma_dtpn = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, ma_dtpnCol), "");
                string so_ku = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, so_kuCol), "");
                string so_hd = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, so_hdCol), "");
                if (ma_tk.Trim().Length == 0 || ma_dtpn.Trim().Length == 0 || so_hd.Trim().Length == 0 || so_ku.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");
                    if (so_ku.Trim().Length == 0)
                        view.SetColumnError(so_kuCol, "Số khế ước không được rổng");
                    if (so_hd.Trim().Length == 0)
                        view.SetColumnError(so_hdCol, "Số hợp đồng không được rổng");

                    return;
                }

                _KTCNKURepo = new KTCNKURepo();
                //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_CNKU obj = new KT_CNKU();

                    obj.SO_HD = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "SO_HD"), "");
                    obj.SO_KU = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "SO_KU"), "");
                    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.NOI_DUNG = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "NOI_DUNG"), "");
                    DateTime? temp = null;
                    if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_KU"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_KU"), DateTime.MinValue);
                    obj.NGAY_KU = temp;
                    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;
                    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.LAI_SUAT_VAY = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "LAI_SUAT_VAY"), 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.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);
                    obj.GT_HD_VND = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "GT_HD_VND"), 0);
                    obj.GT_HD_USD = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "GT_HD_USD"), 0);
                    obj.LTH_DK = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "LTH_DK"), 0);
                    obj.LTH_TK = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "LTH_TK"), 0);
                    obj.LTH_CK = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "LTH_CK"), 0);
                    obj.LQH_DK = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "LQH_DK"), 0);
                    obj.LQH_TK = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "LQH_TK"), 0);
                    obj.LQH_CK = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "LQH_CK"), 0);
                    obj.TONG_LAI = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "TONG_LAI"), 0);
                    temp = null;
                    if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_TONG"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_TONG"), DateTime.MinValue);
                    obj.NGAY_TONG = temp;
                    temp = null;
                    if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_TH"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_TH"), DateTime.MinValue);
                    obj.NGAY_TH = temp;
                    temp = null;
                    if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_QH"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_QH"), DateTime.MinValue);
                    obj.NGAY_QH = temp;

                    _KTCNKURepo.Create(obj);

                }
                //Cũ thì update
                else
                {
                    int id = Utils.CIntDef(view.GetRowCellValue(gridView1.FocusedRowHandle, "ID"), 0);
                    KT_CNKU obj = _KTCNKURepo.GetById(id);
                    if (obj != null)
                    {
                        obj.SO_HD = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "SO_HD"), "");
                        obj.SO_KU = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "SO_KU"), "");
                        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.NOI_DUNG = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "NOI_DUNG"), "");
                        DateTime? temp = null;
                        if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_KU"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_KU"), DateTime.MinValue);
                        obj.NGAY_KU = temp;
                        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;
                        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.LAI_SUAT_VAY = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "LAI_SUAT_VAY"), 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.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);
                        obj.GT_HD_VND = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "GT_HD_VND"), 0);
                        obj.GT_HD_USD = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "GT_HD_USD"), 0);
                        obj.LTH_DK = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "LTH_DK"), 0);
                        obj.LTH_TK = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "LTH_TK"), 0);
                        obj.LTH_CK = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "LTH_CK"), 0);
                        obj.LQH_DK = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "LQH_DK"), 0);
                        obj.LQH_TK = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "LQH_TK"), 0);
                        obj.LQH_CK = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "LQH_CK"), 0);
                        obj.TONG_LAI = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "TONG_LAI"), 0);
                        temp = null;
                        if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_TONG"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_TONG"), DateTime.MinValue);
                        obj.NGAY_TONG = temp;
                        temp = null;
                        if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_TH"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_TH"), DateTime.MinValue);
                        obj.NGAY_TH = temp;
                        temp = null;
                        if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_QH"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_QH"), DateTime.MinValue);
                        obj.NGAY_QH = temp;

                        _KTCNKURepo.Update(obj);
                    }

                }
                Load_Data();
            }
            catch (Exception ex)
            {
                e.Valid = false;
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 partial void InsertKT_CNKU(KT_CNKU instance);
        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)
                {
                    _KTCNKURepo = new KTCNKURepo();
                    KT_CNKU obj = new KT_CNKU();

                    obj.SO_HD = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "SO_HD"), "");
                    obj.SO_KU = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "SO_KU"), "");
                    obj.MA_TK = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MA_TK"), "");
                    obj.MA_DTPN = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MA_DTPN"), "");
                    obj.TEN_DTPN = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TEN_DTPN"), "");
                    obj.NOI_DUNG = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NOI_DUNG"), "");
                    DateTime? temp = null;
                    if (Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_KU"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_KU"), DateTime.MinValue);
                    obj.NGAY_KU = temp;
                    temp = null;
                    if (Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_HD"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_HD"), DateTime.MinValue);
                    obj.NGAY_HD = temp;
                    temp = null;
                    if (Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_DH"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_DH"), DateTime.MinValue);
                    obj.NGAY_DH = temp;
                    obj.LAI_SUAT_VAY = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "LAI_SUAT_VAY"), 0);
                    obj.DUNO_VND = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DUNO_VND"), 0);
                    obj.DUCO_VND = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DUCO_VND"), 0);
                    obj.PSNO_VND = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "PSNO_VND"), 0);
                    obj.PSCO_VND = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "PSCO_VND"), 0);
                    obj.CKNO_VND = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "CKNO_VND"), 0);
                    obj.CKCO_VND = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "CKCO_VND"), 0);
                    obj.TY_GIA_NO = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TY_GIA_NO"), 0);
                    obj.TY_GIA_CO = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TY_GIA_CO"), 0);
                    obj.DANH_DAU = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DANH_DAU"), "");
                    obj.TRANG_THAI = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TRANG_THAI"), 0);
                    obj.GT_HD_VND = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "GT_HD_VND"), 0);
                    obj.GT_HD_USD = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "GT_HD_USD"), 0);
                    obj.LTH_DK = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "LTH_DK"), 0);
                    obj.LTH_TK = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "LTH_TK"), 0);
                    obj.LTH_CK = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "LTH_CK"), 0);
                    obj.LQH_DK = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "LQH_DK"), 0);
                    obj.LQH_TK = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "LQH_TK"), 0);
                    obj.LQH_CK = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "LQH_CK"), 0);
                    obj.TONG_LAI = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TONG_LAI"), 0);
                    temp = null;
                    if (Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_TONG"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_TONG"), DateTime.MinValue);
                    obj.NGAY_TONG = temp;
                    temp = null;
                    if (Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_TH"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_TH"), DateTime.MinValue);
                    obj.NGAY_TH = temp;
                    temp = null;
                    if (Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_QH"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_QH"), DateTime.MinValue);
                    obj.NGAY_QH = temp;

                    _KTCNKURepo.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);
            }
        }