/******************************************************************************* * Copyright(c) 2014 dongke All rights reserved. / Confidential * 类的信息: * 1.程序名称:F_MST_0402.cs * 2.功能描述:数据字典 * 编辑履历: * 作者 日期 版本 修改内容 * 任海 2014/09/15 1.00 新建 *******************************************************************************/ using System; using System.Data; using System.Text; using System.Windows.Forms; using System.Collections; using Dongke.IBOSS.PRD.Basics.BaseResources; using Dongke.IBOSS.PRD.Client.CommonModule; using Dongke.IBOSS.PRD.Basics.BaseControls; using Dongke.IBOSS.PRD.WCF.Proxys; using Dongke.IBOSS.PRD.Basics.Library; using Dongke.IBOSS.PRD.Client.DataModels; namespace Dongke.IBOSS.PRD.Client.SystemModule { /// /// 数据字典 /// public partial class F_MST_0402 : FormBase { #region 成员变量 // 窗体的单例模式 private static F_MST_0402 _instance; // 相应节点数据 private string _dictionaryType; // 有效标识 private byte _valueFalg; // 数据字典Treeview改变前的状态 private Hashtable _nodesStatus = new Hashtable(); // 数据字典TreeView选中节点的路径 private string _selectNodeFullPath; // 数据字典TreeView选中节点的名称 //private string _selectNode; // 字典数据源 private DataTable _dtSourse; #endregion #region 属性 /// /// 数据字典TreeView改变前的状态 /// public Hashtable NodesStatus { get { return _nodesStatus; } set { _nodesStatus = value; } } /// /// 数据字典TreeView选中节点的路径 /// public string SelectNodeFullPath { get { return _selectNodeFullPath; } set { _selectNodeFullPath = value; } } #endregion #region 构造函数 /// /// 构造函数 /// public F_MST_0402() { InitializeComponent(); // 窗口标题 this.Text = FormTitles.F_MST_0402; // 按钮 this.btnSave.Text = ButtonText.BTN_SAVE; this.btnCancel.Text = ButtonText.BTN_CLOSE; } #endregion #region 单例模式 /// /// 单例模式,防止重复创建窗体 /// /// public static F_MST_0402 Instance { get { if (_instance == null) { _instance = new F_MST_0402(); } return _instance; } } #endregion #region 事件 /// /// 页面加载 /// /// /// private void F_MST_0402_Load(object sender, EventArgs e) { try { // 屏蔽根据数据源的实际情况,自动生成datagridview表格列 this.dgvDataDictionary.AutoGenerateColumns = false; this.btnSave.Enabled = false; //生成树形结构 this.CreateDataDictionaryTree(); this.GetAllDataDictionary(); // 窗体显示的时候鼠标停在第一个子节点 this.tvwDictionaryType.SelectedNode = tvwDictionaryType.Nodes[0].Nodes[0]; this.dgvDataDictionary.IsSetInputColumnsColor = true; //权限控制 FormPermissionManager.FormPermissionControl(this.Name, this, LogInUserInfo.CurrentUser.CurrentUserEntity.UserRightData, LogInUserInfo.CurrentUser.CurrentUserEntity.FunctionData); this.dgvDataDictionary.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect; } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 点击窗体右上方关闭按钮 /// /// /// private void FrmDataDictionary_FormClosing(object sender, FormClosingEventArgs e) { try { // 关闭的时候需要判断是否有数据变化 if (DataJudge.IsChange((DataTable)this.dgvDataDictionary.DataSource)) { DialogResult dialogResult = MessageBox.Show(Messages.MSG_CMN_Q001, this.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); // 保存改变的数据 if (dialogResult == DialogResult.Yes) { // 异步处理 int result = (int)DoAsync(new BaseAsyncMethod(SaveDataDictionaryData)); // 如果保存出错,不关闭窗体 if (Convert.ToInt32(result) <= Constant.INT_IS_ZERO) { e.Cancel = true; MessageBox.Show(string.Format(Messages.MSG_CMN_W001, "数据字典", "保存"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show(string.Format(Messages.MSG_CMN_I001, "数据字典", "保存"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } } else if (dialogResult == DialogResult.Cancel) { e.Cancel = true; } //选择否回滚 else { ((DataTable)this.dgvDataDictionary.DataSource).RejectChanges(); } } } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 窗体关闭后,释放单例资源 /// /// /// private void FrmDataDictionary_FormClosed(object sender, FormClosedEventArgs e) { _instance = null; } /// /// 节点收缩之后,显示关闭图标 /// /// /// private void tvwDictionaryType_AfterCollapse(object sender, TreeViewEventArgs e) { e.Node.SelectedImageIndex = Constant.INT_IS_ZERO; e.Node.ImageIndex = Constant.INT_IS_ZERO; } /// /// 节点展开之后,显示打开图标 /// /// /// private void tvwDictionaryType_AfterExpand(object sender, TreeViewEventArgs e) { e.Node.SelectedImageIndex = Constant.INT_IS_TWO; e.Node.ImageIndex = Constant.INT_IS_TWO; } /// /// 选择数据字典TreeView中的节点前判断,如果数据有改变提示保存 /// /// /// private void tvwDictionaryType_BeforeSelect(object sender, TreeViewCancelEventArgs e) { try { this.dgvDataDictionary.AllowUserToAddRows = true; // 判断数据是否被修改过,修改过需要提示保存消息 if (DataJudge.IsChange((DataTable)this.dgvDataDictionary.DataSource)) { DialogResult dialogResult = MessageBox.Show(Messages.MSG_CMN_Q001, this.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); // 保存修改内容 if (dialogResult == DialogResult.Yes) { // 异步处理 int result = (int)DoAsync(new BaseAsyncMethod(SaveDataDictionaryData)); // 如果保存出错,不关闭窗体 if (Convert.ToInt32(result) > Constant.INT_IS_ZERO) { MessageBox.Show(string.Format(Messages.MSG_CMN_I001, "数据字典", "保存"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { this.btnSave.Enabled = false; MessageBox.Show(string.Format(Messages.MSG_CMN_W001, "数据字典", "保存"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else if (dialogResult == DialogResult.Cancel) { e.Cancel = true; } //选择否回滚 else { ((DataTable)this.dgvDataDictionary.DataSource).RejectChanges(); } } } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 选择树中任意值,右边数据跟着变动 /// /// /// private void tvwDictionaryType_AfterSelect(object sender, TreeViewEventArgs e) { try { this.lblDictionaryType.Text = "当前选择的字典类型:" + e.Node.Text; //判断父节点是否存在,表格联动 TreeNode node = tvwDictionaryType.SelectedNode.Parent; if (node != null) { this.dgvDataDictionary.ReadOnly = false; this._dtSourse.AcceptChanges(); this.RefreshData(); } else { this.dgvDataDictionary.DataSource = null; this.dgvDataDictionary.ReadOnly = true; this.dgvDataDictionary.AllowUserToAddRows = false; } } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 数据字典信息输入格式控制 /// /// /// private void dgvDataDictionary_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { try { // 只对文本框做控制 if (e.Control is DataGridViewTextBoxEditingControl) { DataGridViewTextBoxEditingControl EditingControl = (DataGridViewTextBoxEditingControl)e.Control; EditingControl.KeyPress += new KeyPressEventHandler(dgvDataDictionary_KeyPress); } } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 选中新行时,设置默认值 /// /// /// private void dgvDataDictionary_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e) { // 设置有效属性为选中状态 e.Row.Cells["ValueFlag"].Value = Constant.INT_IS_ONE; // 增加一行时,给字典类型赋值 e.Row.Cells["DictionaryType"].Value = tvwDictionaryType.SelectedNode.Tag.ToString(); e.Row.Cells["DisplayNo"].Value = e.Row.Index + 1; } /// /// 屏蔽除了数字以外的输入(显示顺序) /// /// /// private void dgvDataDictionary_KeyPress(object sender, KeyPressEventArgs e) { // 显示顺序列只允许输入数字和回格 if ("DisplayNo".Equals(this.dgvDataDictionary.Columns[this .dgvDataDictionary.CurrentCell.ColumnIndex].Name)) { if (e.KeyChar != '\b' && !char.IsDigit(e.KeyChar)) { e.Handled = true; } } } /// /// 检查是否有重复的字典名称以及将为输入顺序设置成0 /// /// /// private void dgvDataDictionary_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { try { if (!dgvDataDictionary.CurrentRow.IsNewRow) { DataGridViewRow rowItem = dgvDataDictionary.Rows[e.RowIndex]; DataGridViewColumn columnItem = dgvDataDictionary.Columns[e.ColumnIndex]; object columnvalue = rowItem.Cells[columnItem.Name].EditedFormattedValue; // 字典名称 if ("DictionaryValue".Equals(columnItem.Name)) { if (columnvalue != null || !string.IsNullOrEmpty(columnvalue.ToString().Trim())) { if (!Utility.IsUnique(columnvalue.ToString(), dgvDataDictionary.CurrentRow.Index, this.dgvDataDictionary, "DictionaryValue")) { // 单元格的错误消息 this.dgvDataDictionary.CurrentRow.ErrorText = string.Format( Messages.MSG_CMN_W006, "字典名称"); e.Cancel = true; this.btnSave.Enabled = false; return; } } } // 清除单元格的错误消息 this.dgvDataDictionary.CurrentRow.ErrorText = string.Empty; this.dgvDataDictionary.IsSetInputColumnsColor = true; // 显示顺序为空时设置为0 if ("DisplayNo".Equals(columnItem.Name) && string.IsNullOrEmpty(columnvalue.ToString())) { dgvDataDictionary.CurrentCell.Value = Constant.INT_IS_ZERO; dgvDataDictionary.RefreshEdit(); } } } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 单元格校验:根据数据是否变化,设置保存按钮的可用状态 /// /// /// private void dgvDataDictionary_CellValidated(object sender, DataGridViewCellEventArgs e) { try { this.SetSaveBtnStatus(); } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 行校验,不能为空 /// /// /// private void dgvDataDictionary_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { try { // 按Esc键时不校验 if (!dgvDataDictionary.IsCurrentRowDirty) { this.dgvDataDictionary.IsSetInputColumnsColor = true; return; } DataGridViewRow row = dgvDataDictionary.CurrentRow; if (!row.IsNewRow) { // 数据字典类型不能为空 if (row.Cells["DictionaryValue"].Value == null || string.IsNullOrEmpty(row.Cells["DictionaryValue"].Value.ToString().Trim())) { // 单元格的错误消息 this.dgvDataDictionary.CurrentRow.ErrorText = string.Format(Messages.MSG_CMN_W005, "字典名称"); e.Cancel = true; this.btnSave.Enabled = false; return; } // 显示顺序不能为空 if (row.Cells["DisplayNo"].Value == null || string.IsNullOrEmpty(row.Cells["DisplayNo"].Value.ToString().Trim())) { this.dgvDataDictionary.CurrentRow.ErrorText = string.Format(Messages.MSG_CMN_W005, "显示顺序"); e.Cancel = true; this.btnSave.Enabled = false; return; } // 过滤掉数据字典名称,备注首尾的空格 row.Cells["DictionaryValue"].Value = row.Cells["DictionaryValue"].Value.ToString().Trim(); row.Cells["Remarks"].Value =row.Cells["Remarks"].Value==null?"": row.Cells["Remarks"].Value.ToString().Trim(); // 清除单元格的错误消息 this.dgvDataDictionary.CurrentRow.ErrorText = string.Empty; } } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 行校验:根据数据是否变化,设置保存按钮的可用状态 /// /// /// private void dgvDataDictionary_RowValidated(object sender, DataGridViewCellEventArgs e) { try { this.SetSaveBtnStatus(); } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 增加一行,自动计算显示顺序 /// /// /// private void Table_SelectNewRow(object sender, DataTableNewRowEventArgs e) { DataTable itemTable = sender as DataTable; //判断最大值 if (itemTable != null) { int maxValue = Utility.GetMaxValue(itemTable.DefaultView.ToTable(), "DisplayNo"); if (maxValue < Constant.INT_IS_ZERO) { maxValue = Constant.INT_IS_ZERO; } // 判断显示顺序 if (Constant.S_DISPLAYNO_MAX < maxValue + Constant.INT_IS_ONE) { e.Row["DisplayNo"] = maxValue; } else { e.Row["DisplayNo"] = ++maxValue; } e.Row["DictionaryType"] = tvwDictionaryType.SelectedNode.Tag.ToString(); } } /// /// 根据是否选中停用数据,加载数据 /// /// /// private void chkDisplayDisabledData_CheckedChanged(object sender, EventArgs e) { try { this.RefreshData(); this.SetSaveBtnStatus(); this.dgvDataDictionary.IsSetInputColumnsColor = true; } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 保存数据字典数据 /// /// /// private void btnSave_Click(object sender, EventArgs e) { try { // 异步处理 this.btnSave.Enabled = false; this.btnCancel.Enabled = false; int result = (int)DoAsync(new BaseAsyncMethod(SaveDataDictionaryData)); this.btnSave.Enabled = true; this.btnCancel.Enabled = true; // 数据字典数据保存成功 if (result > Constant.INT_IS_ZERO) { RefreshData(); this._dtSourse.AcceptChanges(); this.btnSave.Enabled = false; MessageBox.Show(string.Format(Messages.MSG_CMN_I001, "数据字典", "保存"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } //数据字典数据存在相同的数据 else if (result == (int)Constant.RETURN_IS_EXIST) { MessageBox.Show(string.Format(Messages.MSG_CMN_W006, "数据字典名称"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else { MessageBox.Show(string.Format(Messages.MSG_CMN_W001, "数据字典", "保存"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception ex) { this.btnSave.Enabled = true; this.btnCancel.Enabled = true; // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 点击关闭按钮 /// /// /// private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } /// /// 单元格输入时,给自动生成的行设置颜色 /// /// /// private void dgvDataDictionary_UserAddedRow(object sender, DataGridViewRowEventArgs e) { this.dgvDataDictionary.IsSetInputColumnsColor = true; } /// /// 排序时设置列表的颜色 /// /// private void dgvDataDictionary_Sorted(object sender, EventArgs e) { this.dgvDataDictionary.IsSetInputColumnsColor = true; } #endregion #region 私有方法 /// /// 数据字典左侧树的数据取得 /// /// private DataSet GetDataDictionaryData() { try { return SystemModuleProxy.Service.GetDataDictionaryTreeData(); } catch (Exception ex) { throw ex; } } /// /// 根据取得的数据字典数据,生成树形结构 /// private void CreateDataDictionaryTree() { try { // 取得数据字典数据 DataSet dataDictionaryData = (DataSet)DoAsync(new BaseAsyncMethod(GetDataDictionaryData)); // 没有数据字典数据时,直接返回 if (dataDictionaryData == null || dataDictionaryData.Tables[0].Rows.Count < Constant.INT_IS_ONE) { return; } if (this.tvwDictionaryType.Nodes.Count > Constant.INT_IS_ZERO) { // 添加节点前,保存状态 SaveTreeNodesStatus(this.tvwDictionaryType.Nodes); } // 将所有节点清除 this.tvwDictionaryType.Nodes.Clear(); //生成树形结构 if (dataDictionaryData != null) { if (dataDictionaryData.Tables.Count >= Constant.INT_IS_TWO) { foreach (DataRow newRootRow in dataDictionaryData.Tables[0].Rows) { TreeNode rootNode = new TreeNode(); rootNode.Name = newRootRow["DataDictionaryClass"].ToString().Trim(); rootNode.Text = newRootRow["DataDictionaryClassName"].ToString().Trim(); rootNode.Tag = newRootRow["DataDictionaryClass"].ToString().Trim(); string strWhere = "DataDictionaryClass ='" + newRootRow["DataDictionaryClass"].ToString() + "'"; DataRow[] dataDictionaryRows = dataDictionaryData.Tables[1].Select(strWhere, "DisplayNo"); // 显示子节点 InitTreeView(dataDictionaryData.Tables[1], dataDictionaryRows, rootNode); this.tvwDictionaryType.Nodes.Add(rootNode); } } } this.dgvDataDictionary.IsSetInputColumnsColor = true; } catch (Exception ex) { throw ex; } } /// /// 递归显示数据字典树形目录 /// /// 数据字典数据源 /// /// private void InitTreeView(DataTable dtTable, DataRow[] rows, TreeNode node) { try { foreach (DataRow row in rows) { TreeNode sNode = new TreeNode(); // 新建树节点并添加 sNode = new TreeNode(); sNode.Text = row["DataDictionaryTypeName"].ToString().Trim(); sNode.Name = row["DataDictionaryType"].ToString().Trim(); sNode.Tag = row["DataDictionaryType"].ToString().Trim(); //节点图像索引 sNode.ImageIndex = Constant.INT_IS_ONE; sNode.SelectedImageIndex = Constant.INT_IS_ONE; node.Nodes.Add(sNode); } } catch (Exception ex) { throw ex; } } /// /// 加入节点在原有树中 /// private void InitTreeLeafView(DataRow[] rows, TreeNode node) { try { foreach (DataRow row in rows) { TreeNode sNode = null; if (node == null) { return; } else { // 加入节点在原有树中 sNode = node.Nodes.Add(row["DataDictionaryTypeName"].ToString()); sNode.Name = row["DataDictionaryType"].ToString(); sNode.Tag = row["DataDictionaryType"].ToString(); } } } catch (Exception ex) { throw ex; } } /// /// 保存数据字典TreeView的状态 /// /// private void SaveTreeNodesStatus(TreeNodeCollection nodes) { try { foreach (TreeNode node in nodes) { // 保存所有节点折叠,展开状态 if (node.IsExpanded) { this.NodesStatus[node.FullPath] = true; } else { this.NodesStatus.Remove(node.FullPath); } // 当节点处于选中状态时获取根树节点到当前树节点的路径 if (node.IsSelected) { SelectNodeFullPath = node.FullPath; } SaveTreeNodesStatus(node.Nodes); } } catch (Exception ex) { throw ex; } } /// /// 还原保存前产品类型TreeView的状态 /// /// 节点 private void SetTreeNodesStatus(TreeNodeCollection nodes) { try { foreach (TreeNode node in nodes) { if (this.NodesStatus[node.FullPath] != null) { // 展开树节点 node.Expand(); } // 节点路径和前选中的路径相同 设置节点为选中状态 if (node.FullPath == SelectNodeFullPath) { this.tvwDictionaryType.SelectedNode = node; } SetTreeNodesStatus(node.Nodes); } } catch (Exception ex) { throw ex; } } /// /// 刷新数据 /// private object RefreshData() { try { // 获取相应节点数据 this._dictionaryType = this.tvwDictionaryType.SelectedNode.Tag.ToString(); // 获取是否查询正常数据的标识 this._valueFalg = Convert.ToByte(this.chkDisplayDisabledData.Checked); // 获取查询的数据集 base.DataSource = (DataSet)DoAsync(new BaseAsyncMethod(GetDictionaryDate)); // 获取查询的数据集 StringBuilder sbFilter = new StringBuilder(); sbFilter.Append(" 1=1"); if (this._valueFalg == Constant.INT_IS_ZERO) { sbFilter.Append(" and ValueFlag = 1"); } if (this._dictionaryType != "") { sbFilter.Append(" and DictionaryType = '" + _dictionaryType + "'"); } this._dtSourse.DefaultView.RowFilter = sbFilter.ToString(); if (this._dtSourse.DefaultView.ToTable().Rows.Count > Constant.INT_IS_ZERO) { // 选中新行时,自动显示顺序 this._dtSourse.TableNewRow += new DataTableNewRowEventHandler(Table_SelectNewRow); this.dgvDataDictionary.DataSource = this._dtSourse; this.dgvDataDictionary.IsSetInputColumnsColor = true; } else { this._dtSourse.TableNewRow += new DataTableNewRowEventHandler(Table_SelectNewRow); this.dgvDataDictionary.DataSource = this._dtSourse; this.dgvDataDictionary.IsSetInputColumnsColor = true; } return Constant.INT_IS_ZERO; } catch (Exception ex) { throw ex; } } /// /// 设置保存按钮的可用状态 /// private void SetSaveBtnStatus() { if (DataJudge.IsChange((DataTable)this.dgvDataDictionary.DataSource)) { this.btnSave.Enabled = true; } else { this.btnSave.Enabled = false; } } /// /// 保存数据字典数据 /// /// 受影响行数 private object SaveDataDictionaryData() { try { // 获取当前dgvDataDictionary数据源 DataTable dataDictionaryData = (DataTable)this.dgvDataDictionary.DataSource; int result = SystemModuleProxy.Service.SaveDictionaryData(dataDictionaryData); return result; } catch (Exception ex) { throw ex; } } /// /// 获取数据字典信息 /// /// private object GetDictionaryDate() { try { return SystemModuleProxy.Service.GetDictionaryData(_valueFalg, _dictionaryType); } catch (Exception ex) { throw ex; } } /// /// 获取数据字典的全部数据 /// /// 用户信息 /// /// /// 2014.10.31 任海 新建 /// private void GetAllDataDictionary() { try { DataSet dsResultAccount = (DataSet)DoAsync(new BaseAsyncMethod(() => { return SystemModuleProxy.Service.GetAllDataDictionaryInfo(); })); this._dtSourse = dsResultAccount.Tables[0]; } catch (Exception ex) { throw ex; } } #endregion } }