private void Save_Data(bool msg)
        {
            try
            {
                _KTBHRepo = new KTBHRepo();
                int i = 0;
                foreach (int pos in _listUpdate)
                {
                    int id = Utils.CIntDef(gridView1.GetRowCellValue(pos, "ID"), 0);
                    KT_BH obj = _KTBHRepo.GetById(id);
                    if (obj != null)
                    {
                        obj.IMEI = Utils.CStrDef(gridView1.GetRowCellValue(pos, "IMEI"), "");
                        obj.MA_DM = Utils.CStrDef(gridView1.GetRowCellValue(pos, "MA_DM"), "");
                        obj.TEN_DM = Utils.CStrDef(gridView1.GetRowCellValue(pos, "TEN_DM"), "");
                        obj.DVT = Utils.CStrDef(gridView1.GetRowCellValue(pos, "DVT"), "");                        
                        obj.SO_HOADON = Utils.CStrDef(gridView1.GetRowCellValue(pos, "SO_HOADON"), "");
                        DateTime? temp = null;
                        if (Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_HET_BH"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_HET_BH"), DateTime.MinValue);
                        obj.NGAY_HET_BH = 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;
                        obj.MA_KH = Utils.CStrDef(gridView1.GetRowCellValue(pos, "MA_KH"), "");
                        obj.TEN_KH = Utils.CStrDef(gridView1.GetRowCellValue(pos, "TEN_KH"), "");
                        obj.DIEN_THOAI = Utils.CStrDef(gridView1.GetRowCellValue(pos, "DIEN_THOAI"), "");
                        obj.DIA_CHI = Utils.CStrDef(gridView1.GetRowCellValue(pos, "DIA_CHI"), "");
                        obj.MA_SO_THUE = Utils.CStrDef(gridView1.GetRowCellValue(pos, "MA_SO_THUE"), "");
                        if (Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_NHAP"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_NHAP"), DateTime.MinValue);
                        obj.NGAY_NHAP = temp;
                        if (Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_XUAT"), DateTime.MinValue) != DateTime.MinValue)
                            temp = Utils.CDateDef(gridView1.GetRowCellValue(pos, "NGAY_XUAT"), DateTime.MinValue);
                        obj.NGAY_XUAT = temp;
                        obj.NOIDUNG_BH = Utils.CStrDef(gridView1.GetRowCellValue(pos, "NOIDUNG_BH"), "");
                        obj.TON_DK = Utils.CDblDef(gridView1.GetRowCellValue(pos, "TON_DK"), 0);
                        obj.DANH_DAU = Utils.CStrDef(gridView1.GetRowCellValue(pos, "DANH_DAU"), "");
                        obj.TRANG_THAI = Utils.CIntDef(gridView1.GetRowCellValue(pos, "TRANG_THAI"), 0);
                        obj.NHAP_TK = Utils.CDblDef(gridView1.GetRowCellValue(pos, "NHAP_TK"), 0);
                        obj.XUAT_TK = Utils.CDblDef(gridView1.GetRowCellValue(pos, "XUAT_TK"), 0);
                        obj.TON_CK = Utils.CDblDef(gridView1.GetRowCellValue(pos, "TON_CK"), 0);

                        _KTBHRepo.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 Load_Data()
 {
     try
     {
         _KTBHRepo = new KTBHRepo();
         gridData.DataSource = _KTBHRepo.GetAll();
         //_db = new dbVstoreAppDataContext(Const.builder.ConnectionString);
         //gridData.DataSource = null;
         //gridData.DataSource = _db.KT_DMHHs;
         //gridData.RefreshDataSource();
         //gridView1.RefreshData();
     }
     catch (Exception) { }
 }
        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 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);
                _KTBHRepo = new KTBHRepo();
                _KTBHRepo.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)
                {
                    _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);
            }
        }
 private void Save_Tick()
 {
     try
     {
         _KTBHRepo = new KTBHRepo();
         int id = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID").ToString(), 0);
         KT_BH obj = _KTBHRepo.GetById(id);
         if (obj != null)
         {
             obj.DANH_DAU = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DANH_DAU"), "").Trim() == "T" ? "" : "T";
             _KTBHRepo.Update(obj);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }