#region using System; using System.Data; using System.Windows.Forms; using CSFrameworkV5.Business; using CSFrameworkV5.Common; using CSFrameworkV5.Core; using CSFrameworkV5.Library.CommonClass; using DevExpress.XtraEditors; using DevExpress.XtraGrid; #endregion namespace CSFrameworkV5.Library { /// /// 数据编辑窗体基类 /// public partial class frmBaseEditor : frmBase, IEditorForm { protected EditorType _EditorType = EditorType.None; /// /// 父级数据查询窗体 /// protected frmBaseEditorQuery _QueryForm; /// /// 表格对象 /// protected ISummaryView _SummaryView; /// /// 数据操作状态 /// protected UpdateType _UpdateType = UpdateType.None; public frmBaseEditor() { InitializeComponent(); } /// /// 是否新增模式 /// public bool IsAddMode => _UpdateType == UpdateType.Add; /// /// 是否新增/修改模式 /// public bool IsAddOrEditMode => _UpdateType == UpdateType.Add || _UpdateType == UpdateType.Modify; /// /// 是否修改模式 /// public bool IsEditMode => _UpdateType == UpdateType.Modify; /// /// 是否查看模式 /// public bool IsViewMode => _UpdateType == UpdateType.None; /// /// 当前数据编辑窗体的主键值 /// public virtual string GetKeyValue() { return ""; } /// /// 检查是否有选择一条记录. /// protected void AssertFocusedRow() { var ret = _SummaryView == null || _SummaryView.IsValidRowHandle(_SummaryView .FocusedRowHandle) == false; if (ret) throw new Exception("您没有选择记录,操作取消!"); } private void btnMoveFirst_Click(object sender, EventArgs e) { DoMoveFirst(sender); } private void btnMoveLast_Click(object sender, EventArgs e) { DoMoveLast(sender); } private void btnMoveNext_Click(object sender, EventArgs e) { DoMoveNext(sender); } private void btnMovePrev_Click(object sender, EventArgs e) { DoMovePrior(sender); } private bool ButtonAuthorized(int action) { return _QueryForm.ButtonAuthorized(action); } /// /// 按钮状态发生变化 /// protected virtual void ButtonStateChanged(UpdateType currentState) { // } /// /// 新增记录 /// /// public virtual void DoAdd(object sender) { _UpdateType = UpdateType.Add; SetEditMode(); ButtonStateChanged(_UpdateType); } protected void DoAfterDelete(bllBaseDataDict bll, string primaryKey) { DataDictCache.Cache.SyncDeleteCacheRow(bll.SummaryTableName, bll.KeyFieldName, primaryKey); DataDictObservers.Notify(bll.SummaryTableName); } /// /// 提交数据成功后处理的扩展方法 /// /// protected virtual void DoAfterSave(bllBaseDataDict bll, DataTable dataTable) { DataDictCache.Cache.SyncAddOrUpdateCache(dataTable.Rows[0], _UpdateType, bll.SummaryTableName, bll.KeyFieldName); //同步更新缓存数据 DataDictObservers.Notify(dataTable.TableName); } protected virtual bool DoBeforeDelete(bllBaseBusiness bll, DataRow row) { if (bll.IsOwnerChange(row) == false) { Msg.Warning( "您不能删除别人创建的单据!\r\n1.您与制单人不构成上下级关系\r\n2.制单人是分配临时的权限!"); return false; } if (bll.IsApproved(row)) { Msg.Warning("单据已经审核,不能删除!"); return false; } return true; } protected virtual bool DoBeforeDelete(bllBaseDataDict bll, ref string keyValue) { AssertFocusedRow(); //检查是否选择一条记录 //调用业务逻辑类删除记录 var summary = _SummaryView.GetDataRow(_SummaryView.FocusedRowHandle); keyValue = summary[bll.KeyFieldName].ToStringEx(); //主键值 return Msg.AskQuestion("确定要删除主键为 " + keyValue + " 的资料?"); } protected virtual bool DoBeforeEdit(bllBaseBusiness bll, DataRow row) { if (bll.IsOwnerChange(row) == false) { Msg.Warning( "您不能修改别人创建的单据!\r\n1.您与制单人不构成上下级关系\r\n2.制单人是分配临时的权限!"); return false; } if (bll.IsApproved(row)) { Msg.Warning("单据已经审核,不能修改!"); return false; } return true; } protected virtual bool DoBeforeEdit(bllBaseDataDict bll, DataRow row) { if (bll.IsOwnerChange(row) == false) { Msg.Warning("您不能修改别人的资料!\r\n1.您与创建人不构成上下级关系\r\n2.创建人是分配临时的权限!"); return false; } return true; } /// /// 取消新增或修改 /// /// public virtual void DoCancel(object sender) { if (!Msg.AskQuestion("要取消修改吗?")) return; try { frmWaitingEx.ShowMe(this); _UpdateType = UpdateType.None; SetViewMode(); ButtonStateChanged(_UpdateType); if (_SummaryView.FocusedRowHandle >= 0) DoViewContent(sender); //设置查看模式,当前记录 else SetViewMode(); } catch (Exception ex) { LogUserOperate.Write(ex); Msg.ShowException(ex); } finally { frmWaitingEx.HideMe(this); } } public virtual void DoDelete(object sender) { // } /// /// 修改数据 /// /// public virtual void DoEdit(object sender) { _UpdateType = UpdateType.Modify; SetEditMode(); ButtonStateChanged(_UpdateType); } protected virtual void DoPrint(object sender) { Msg.Warning("没有实现打印功能,请参考【销售订单】界面的打印功能!"); } /// /// 保存数据 /// /// public virtual void DoSave(object sender) { // } protected virtual void DoViewContent(object sender) { ButtonStateChanged(_UpdateType); } protected virtual string GetStateName() { if (UpdateType.Add == _UpdateType) return "(新增模式)"; if (UpdateType.Modify == _UpdateType) return "(修改模式)"; return "(查看模式)"; } /// /// 设置为编辑模式 /// 数据操作两种状态.1:数据修改状态 2:查看数据状态 /// protected virtual void SetEditMode() { btnAdd.Enabled = false; btnDel.Enabled = false; btnEdit.Enabled = false; btnView.Enabled = false; btnPrint.Enabled = false; btnSave.Enabled = true; btnCancel.Enabled = true; pnlNavigator.Enabled = false; } /// /// 设置Grid自定义按钮(Add,Insert,Delete)状态 /// protected void SetGridCustomButtonAccessable(GridControl gridControl, bool value) { var custom = gridControl.EmbeddedNavigator.Buttons.CustomButtons; foreach (NavigatorCustomButton btn in custom) btn.Enabled = value; } /// /// 设置为查看模式 /// 数据操作两种状态.1:数据修改状态 2:查看数据状态 /// protected virtual void SetViewMode() { btnAdd.Enabled = ButtonAuthorized(ButtonAuthority.ADD); btnDel.Enabled = ButtonAuthorized(ButtonAuthority.DELETE); btnEdit.Enabled = ButtonAuthorized(ButtonAuthority.EDIT); btnView.Enabled = ButtonAuthorized(ButtonAuthority.VIEW); btnPrint.Enabled = ButtonAuthorized(ButtonAuthority.PRINT); btnSave.Enabled = false; btnCancel.Enabled = false; pnlNavigator.Enabled = _SummaryView.RowCount > 0; } /// /// 在保存时有此情况发生: 不会更新最后一个编辑框里的数据! /// 当移除焦点后会更新输入框的数据. /// protected void UpdateLastControl() { try { if (ActiveControl == null) return; var ctl = ActiveControl; txtFocusForSave.Focus(); ActiveControl = ctl; } catch (Exception ex) { Msg.ShowException(ex); } } #region 8个按钮Click事件 private void btnAdd_Click(object sender, EventArgs e) { //DoAdd(btnAdd); //注释掉:由派生类实现Click事件 } private void btnDel_Click(object sender, EventArgs e) { //DoDelete(btnDel);//注释掉:由派生类实现Click事件 } private void btnEdit_Click(object sender, EventArgs e) { //DoEdit(btnEdit);//注释掉:由派生类实现Click事件 } private void btnView_Click(object sender, EventArgs e) { //DoViewContent(btnView);//注释掉:由派生类实现Click事件 } private void btnSave_Click(object sender, EventArgs e) { //DoSave(btnSave);//注释掉:由派生类实现Click事件 } private void btnCancel_Click(object sender, EventArgs e) { //DoCancel(btnCancel);//注释掉:由派生类实现Click事件 } private void btnPrint_Click(object sender, EventArgs e) { //DoPrint(btnPrint);//注释掉:由派生类实现Click事件 } private void btnClose_Click(object sender, EventArgs e) { //注释掉:由派生类实现Click事件 } #endregion #region Summary数据导航功能 protected void UpdateNavigatorState() { if (_SummaryView.RowCount > 0) { var R = _SummaryView.RowCount; var F = _SummaryView.FocusedRowHandle; btnMoveFirst.Enabled = F > 0; btnMoveLast.Enabled = F < R - 1; btnMoveNext.Enabled = F < R - 1; btnMovePrev.Enabled = F > 0; } } /// /// 移到第一条记录 /// protected virtual void DoMoveFirst(object sender) { if (_SummaryView == null) return; _SummaryView.MoveFirst(); DoViewContent(null); } /// /// 移到上一条记录 /// protected virtual void DoMovePrior(object sender) { if (_SummaryView == null) return; _SummaryView.MovePrior(); DoViewContent(null); } /// /// 移到下一条记录 /// protected virtual void DoMoveNext(object sender) { if (_SummaryView == null) return; _SummaryView.MoveNext(); DoViewContent(null); } /// /// 移到最后一条记录 /// protected virtual void DoMoveLast(object sender) { if (_SummaryView == null) return; _SummaryView.MoveLast(); DoViewContent(null); } #endregion #region Set Editors Accessable protected void SetEditorEnable(TextEdit editor, bool enable, bool setBackgroundColor) { DataBinder.SetEditorEnable(editor, enable, setBackgroundColor); } /// /// 控制[明细编辑页面]上的所有可输入控件是否可编辑. /// protected virtual void SetDetailEditorsAccessable(Control panel, bool value) { if (panel != null) DataBinder.SetControlAccessableCycle(panel, value); } /// /// 设置容器内所有可输入控件的状态.ReadOnly or Enable = false/true /// /// 容器 /// false/true protected void SetControlAccessable(Control container, bool value) { DataBinder.SetControlAccessable(container, value); } #endregion } }