private void btnOK_Click(object sender, EventArgs e) { 源房缴费明细BindingSource.EndEdit(); string check = payDetail.CheckRules(); if (!string.IsNullOrEmpty(check)) { KryptonMessageBox.Show(check, "数据校验失败"); return; } if (isNew)//新增 { string msg; if (Helper.saveData(context, payDetail, out msg)) { KryptonMessageBox.Show(msg, "成功新增缴费信息"); if (this.Owner is Main) { (this.Owner as Main).RefreshAndLocateTree(payDetail.源房);//刷新TreeView,并定位到yf节点。 } Close(); } else { KryptonMessageBox.Show(msg, "失败"); } } else//编辑 { if (context.ObjectStateManager.GetObjectStateEntry(payDetail).State == EntityState.Unchanged) { Close(); //如果编辑状态下,未做任何修改,则直接退出 } else { string msg; if (Helper.saveData(context, payDetail, out msg)) { KryptonMessageBox.Show(msg, "成功编辑缴费信息"); if (this.Owner is Main) { (this.Owner as Main).RefreshAndLocateTree(payDetail.源房);//刷新TreeView,并定位到yf节点。 } Close(); } else { KryptonMessageBox.Show(msg, "失败"); } } } }
private void btnOK_Click(object sender, EventArgs e) { 源房缴费明细BindingSource.EndEdit(); var changes = Main.context.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted | EntityState.Modified); if (changes.Count() > 0) { //校验 StringBuilder sb = new StringBuilder(); Dictionary <Guid, string> errors = new Dictionary <Guid, string>();//记录错误的ID号,和对应错误信息。 foreach (ObjectStateEntry change in changes) { //校验每一条更改的实体,最后联合显示错误信息。 源房缴费明细 entity = change.Entity as 源房缴费明细; string result = entity.CheckRules(); if (!string.IsNullOrEmpty(result))//证明有错误 { string err = string.Format("-----[{0}][{1}][{2}]:该记录有误-----\r\n{3}", entity.源房.房名, entity.缴费时间.ToShortDateString(), entity.缴费项, result); sb.Append(err); errors.Add(entity.ID, err); } } if (!string.IsNullOrEmpty(sb.ToString())) { highLightRow(errors);//高亮错误行 KryptonMessageBox.Show(sb.ToString(), "数据校验失败"); return; } string msg; if (Helper.saveData(Main.context.源房缴费明细, out msg)) { KryptonMessageBox.Show(msg, "成功保存"); Close(); } else { KryptonMessageBox.Show(msg, "失败"); } } else//未做更改 { Close(); } }