private void Get_Data(KT_DonMuaHang obj, int position)
 {
     try
     {
         obj.STT = Utils.CIntDef(gridView1.GetRowCellValue(position, "STT"), 0);
         obj.LOAI_DH = Utils.CStrDef(gridView1.GetRowCellValue(position, "LOAI_DH"), "");
         obj.SO_HD = Utils.CDblDef(gridView1.GetRowCellValue(position, "SO_HD"), 0);
         obj.MA_KH = Utils.CStrDef(gridView1.GetRowCellValue(position, "MA_KH"), "");
         obj.NGAY_HD = gridView1.GetRowCellValue(position, "NGAY_HD") != null 
             ? Utils.CDateDef(gridView1.GetRowCellValue(position, "NGAY_HD"), DateTime.Now) : obj.NGAY_HD = null;
         obj.NOI_DUNG_HOPDONG = Utils.CStrDef(gridView1.GetRowCellValue(position, "NOI_DUNG_HOPDONG"), "");
         obj.TEN_KH = Utils.CStrDef(gridView1.GetRowCellValue(position, "TEN_KH"), "");
         obj.DIA_CHI_KH = Utils.CStrDef(gridView1.GetRowCellValue(position, "DIA_CHI_KH"), "");
         obj.MST = Utils.CStrDef(gridView1.GetRowCellValue(position, "MST"), "");
         obj.MA_NLH_MUA = Utils.CStrDef(gridView1.GetRowCellValue(position, "MA_NLH_MUA"), "");
         obj.TEN_NLH_MUA = Utils.CStrDef(gridView1.GetRowCellValue(position, "TEN_NLH_MUA"), "");
         obj.DT_NLH_MUA = Utils.CStrDef(gridView1.GetRowCellValue(position, "DT_NLH_MUA"), "");
         obj.MA_NLH_BAN = Utils.CStrDef(gridView1.GetRowCellValue(position, "MA_NLH_BAN"), "");
         obj.TEN_NLH_BAN = Utils.CStrDef(gridView1.GetRowCellValue(position, "TEN_NLH_BAN"), "");
         obj.DT_NLH_BAN = Utils.CStrDef(gridView1.GetRowCellValue(position, "DT_NLH_BAN"), "");
         obj.NGAY_GH = gridView1.GetRowCellValue(position, "NGAY_GH") != null 
             ? Utils.CDateDef(gridView1.GetRowCellValue(position, "NGAY_GH"), DateTime.Now) : obj.NGAY_GH = null;
         obj.MA_DM = Utils.CStrDef(gridView1.GetRowCellValue(position, "MA_DM"), "");
         obj.TEN_DM = Utils.CStrDef(gridView1.GetRowCellValue(position, "TEN_DM"), "");
         obj.DVT = Utils.CStrDef(gridView1.GetRowCellValue(position, "DVT"), "");
         obj.SO_LUONG = Utils.CDblDef(gridView1.GetRowCellValue(position, "DON_GIA"), 0);
         obj.DON_GIA = Utils.CDblDef(gridView1.GetRowCellValue(position, "DON_GIA_VND"), 0);
         obj.THANH_TIEN = Utils.CDblDef(gridView1.GetRowCellValue(position, "THANH_TIEN"), 0);
         obj.TS_GTGT = Utils.CDblDef(gridView1.GetRowCellValue(position, "TS_GTGT"), 0);
         obj.TIEN_THUE = Utils.CDblDef(gridView1.GetRowCellValue(position, "TIEN_THUE"), 0);
         obj.TONG_TIEN = Utils.CDblDef(gridView1.GetRowCellValue(position, "TONG_TIEN_VND"), 0);
         obj.TON_CK_SL = Utils.CDblDef(gridView1.GetRowCellValue(position, "TON_CK_SL"), 0);
         obj.TON_CK_GT = Utils.CDblDef(gridView1.GetRowCellValue(position, "TON_CK_GT"), 0);
         obj.DON_GIA_TON = Utils.CDblDef(gridView1.GetRowCellValue(position, "DON_GIA_TON"), 0);
         obj.GHI_CHU = Utils.CStrDef(gridView1.GetRowCellValue(position, "GHI_CHU"), "");
         obj.TRANG_THAI = Utils.CIntDef(gridView1.GetRowCellValue(position, "TRANG_THAI"), 0);
     }
     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;
                _KTDonMuaHangRepo = new KTDonMuaHangRepo();
                //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_DonMuaHang obj = new KT_DonMuaHang();
                    Get_Data(obj, e.RowHandle);
                    _KTDonMuaHangRepo.Create(obj);

                }
                //Cũ thì update
                else
                {
                    int id = Utils.CIntDef(gridView1.GetRowCellValue(e.RowHandle, "ID").ToString(), 0);
                    KT_DonMuaHang obj = _KTDonMuaHangRepo.GetById(id);
                    if (obj != null)
                    {
                        Get_Data(obj, e.RowHandle);
                        _KTDonMuaHangRepo.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)
         {
             _KTDonMuaHangRepo = new KTDonMuaHangRepo();
             KT_DonMuaHang obj = new KT_DonMuaHang();
             Get_Data(obj, gridView1.FocusedRowHandle);
             _KTDonMuaHangRepo.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);
     }
 }
 partial void DeleteKT_DonMuaHang(KT_DonMuaHang instance);
 partial void UpdateKT_DonMuaHang(KT_DonMuaHang instance);
 partial void InsertKT_DonMuaHang(KT_DonMuaHang instance);