private void Save_Data(bool msg) { try { _KTDIENGIAIRepo = new KTDIENGIAIRepo(); int i = 0; foreach (int pos in _listUpdate) { int id = Utils.CIntDef(gridView1.GetRowCellValue(pos, "ID"), 0); KT_DIENGIAI obj = _KTDIENGIAIRepo.GetById(id); if (obj != null) { obj.DIEN_GIAI1 = Utils.CStrDef(gridView1.GetRowCellValue(pos, "DIEN_GIAI1"), ""); obj.DIEN_GIAI2 = Utils.CStrDef(gridView1.GetRowCellValue(pos, "DIEN_GIAI2"), ""); _KTDIENGIAIRepo.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 gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e) { try { GridView view = sender as GridView; _KTDIENGIAIRepo = new KTDIENGIAIRepo(); //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_DIENGIAI obj = new KT_DIENGIAI(); obj.DIEN_GIAI1 = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DIEN_GIAI1"), ""); obj.DIEN_GIAI2 = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DIEN_GIAI2"), ""); _KTDIENGIAIRepo.Create(obj); } //Cũ thì update else { int id = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "ID"), 0); KT_DIENGIAI obj = _KTDIENGIAIRepo.GetById(id); if (obj != null) { obj.DIEN_GIAI1 = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DIEN_GIAI1"), ""); obj.DIEN_GIAI2 = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "DIEN_GIAI2"), ""); _KTDIENGIAIRepo.Update(obj); } } Load_Data(); } catch (Exception ex) { e.Valid = false; MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); } }