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

                //validation
                GridColumn imeiCol = view.Columns["IMEI"];
                string imei = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, imeiCol), "");                
                if (imei.Trim().Length == 0)
                {
                    e.Valid = false;
                    if (imei.Trim().Length == 0)
                        view.SetColumnError(imeiCol, "Imei không được rổng");
                    
                    return;
                }

                _KTBHRepo = new KTBHRepo();
                //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_BH obj = new KT_BH();

                    obj.IMEI = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "IMEI"), "");
                    obj.MA_DM = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_DM"), "");
                    obj.TEN_DM = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_DM"), "");
                    obj.DVT = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DVT"), "");
                    obj.SO_HOADON = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "SO_HOADON"), "");
                    DateTime? temp = null;
                    if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_HET_BH"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_HET_BH"), DateTime.MinValue);
                    obj.NGAY_HET_BH = 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;
                    obj.MA_KH = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_KH"), "");
                    obj.TEN_KH = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_KH"), "");
                    obj.DIEN_THOAI = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DIEN_THOAI"), "");
                    obj.DIA_CHI = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DIA_CHI"), "");
                    obj.MA_SO_THUE = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_SO_THUE"), "");
                    if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_NHAP"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_NHAP"), DateTime.MinValue);
                    obj.NGAY_NHAP = temp;
                    if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_XUAT"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_XUAT"), DateTime.MinValue);
                    obj.NGAY_XUAT = temp;
                    obj.NOIDUNG_BH = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "NOIDUNG_BH"), "");
                    obj.TON_DK = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "TON_DK"), 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.NHAP_TK = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NHAP_TK"), 0);
                    obj.XUAT_TK = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "XUAT_TK"), 0);
                    obj.TON_CK = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "TON_CK"), 0);

                    _KTBHRepo.Create(obj);

                }
                //Cũ thì update
                else
                {
                    int id = Utils.CIntDef(view.GetRowCellValue(gridView1.FocusedRowHandle, "ID"), 0);
                    KT_BH obj = _KTBHRepo.GetById(id);
                    if (obj != null)
                    {
                        obj.IMEI = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "IMEI"), "");
                        obj.MA_DM = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_DM"), "");
                        obj.TEN_DM = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_DM"), "");
                        obj.DVT = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DVT"), "");
                        obj.MA_CN = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_CN"), "");
                        obj.TINH_TRANG = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TINH_TRANG"), "");
                        obj.TK_HH = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TK_HH"), "");
                        obj.SO_HOADON = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "SO_HOADON"), "");
                        DateTime? temp = null;
                        if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_HET_BH"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_HET_BH"), DateTime.MinValue);
                        obj.NGAY_HET_BH = 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;
                        obj.MA_KH = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_KH"), "");
                        obj.TEN_KH = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_KH"), "");
                        obj.DIEN_THOAI = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DIEN_THOAI"), "");
                        obj.DIA_CHI = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DIA_CHI"), "");
                        obj.MA_SO_THUE = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_SO_THUE"), "");
                        if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_NHAP"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_NHAP"), DateTime.MinValue);
                        obj.NGAY_NHAP = temp;
                        if (Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_XUAT"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(view.GetRowCellValue(e.RowHandle, "NGAY_XUAT"), DateTime.MinValue);
                        obj.NGAY_XUAT = temp;
                        obj.NOIDUNG_BH = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "NOIDUNG_BH"), "");
                        obj.TON_DK = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "TON_DK"), 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.NHAP_TK = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "NHAP_TK"), 0);
                        obj.XUAT_TK = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "XUAT_TK"), 0);
                        obj.TON_CK = Utils.CDblDef(view.GetRowCellValue(e.RowHandle, "TON_CK"), 0);

                        _KTBHRepo.Update(obj);
                    }

                }
                Load_Data();
            }
            catch (Exception ex)
            {
                e.Valid = false;
                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)
                {
                    _KTBHRepo = new KTBHRepo();
                    KT_BH obj = new KT_BH();

                    obj.IMEI = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "IMEI"), "");
                    obj.MA_DM = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MA_DM"), "");
                    obj.TEN_DM = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TEN_DM"), "");
                    obj.DVT = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DVT"), "");
                    obj.SO_HOADON = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "SO_HOADON"), "");
                    DateTime? temp = null;
                    if (Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_HET_BH"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_HET_BH"), DateTime.MinValue);
                    obj.NGAY_HET_BH = 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;
                    obj.MA_KH = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MA_KH"), "");
                    obj.TEN_KH = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TEN_KH"), "");
                    obj.DIEN_THOAI = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DIEN_THOAI"), "");
                    obj.DIA_CHI = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DIA_CHI"), "");
                    obj.MA_SO_THUE = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MA_SO_THUE"), "");
                    if (Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_NHAP"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_NHAP"), DateTime.MinValue);
                    obj.NGAY_NHAP = temp;
                    if (Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_XUAT"), DateTime.MinValue) != DateTime.MinValue)
                        temp = Utils.CDateDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NGAY_XUAT"), DateTime.MinValue);
                    obj.NGAY_XUAT = temp;
                    obj.NOIDUNG_BH = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NOIDUNG_BH"), "");
                    obj.TON_DK = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TON_DK"), 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.NHAP_TK = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NHAP_TK"), 0);
                    obj.XUAT_TK = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "XUAT_TK"), 0);
                    obj.TON_CK = Utils.CDblDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TON_CK"), 0);

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