| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717 |
- /*******************************************************************************
- * Copyright(c) 2018 DongkeSoft All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:F_PM_0906.cs
- * 2.功能描述:新建编辑成型破损原因
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 周兴 2018/3/26 1.00 新建
- *******************************************************************************/
- using System;
- using System.Collections;
- using System.Data;
- using System.Text;
- using System.Windows.Forms;
- using Dongke.IBOSS.PRD.Basics.BaseControls;
- using Dongke.IBOSS.PRD.Basics.BaseResources;
- using Dongke.IBOSS.PRD.Basics.Library;
- using Dongke.IBOSS.PRD.Client.CommonModule;
- using Dongke.IBOSS.PRD.Client.DataModels;
- using Dongke.IBOSS.PRD.WCF.DataModels;
- using Dongke.IBOSS.PRD.WCF.Proxys;
- namespace Dongke.IBOSS.PRD.Client.PMModule
- {
- public partial class F_PM_0906 : FormBase
- {
- #region 成员变量
- private static F_PM_0906 _instance; // 窗体的实例
- private byte _valueFalg; // 有效标识
- private string _scrapType; // 报损类型
- private DataTable _dtSourse; // 数据源
- private Hashtable _nodesStatus = new Hashtable(); // 数据字典Treeview改变前的状态
- private string _selectNodeFullPath; // 数据字典TreeView选中节点的路径
- private DataTable _initDataSource; // 初始加载的数据源
- #endregion
- #region 属性
- /// <summary>
- /// 数据字典TreeView改变前的状态
- /// </summary>
- public Hashtable NodesStatus
- {
- get
- {
- return _nodesStatus;
- }
- set
- {
- _nodesStatus = value;
- }
- }
- /// <summary>
- /// 数据字典TreeView选中节点的路径
- /// </summary>
- public string SelectNodeFullPath
- {
- get
- {
- return _selectNodeFullPath;
- }
- set
- {
- _selectNodeFullPath = value;
- }
- }
- #endregion
- #region 构造函数
- private F_PM_0906()
- {
- InitializeComponent();
- // 窗口标题
- this.Text = FormTitles.F_MST_0906;
- // 按钮
- this.btnSave.Text = ButtonText.BTN_SAVE;
- this.btnCancel.Text = ButtonText.BTN_CLOSE;
- }
- /// <summary>
- /// 单例模式提供实例
- /// </summary>
- /// <returns></returns>
- public static F_PM_0906 Instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = new F_PM_0906();
- }
- return _instance;
- }
- }
- #endregion
- #region 事件
- /// <summary>
- /// 窗体加载事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void F_PM_0906_Load(object sender, EventArgs e)
- {
- try
- {
- // 屏蔽根据数据源的实际情况,自动生成datagridview表格列
- this.dgvScrapReason.AutoGenerateColumns = false;
- this.btnSave.Enabled = false;
- //生成树形结构
- this.CreateScrapTypeTree();
- // 获取报损原因数据
- this.GetScrapTypeData();
- // 窗体显示的时候鼠标停在第一个子节点
- this.tvwScrapType.SelectedNode = tvwScrapType.Nodes[0];
- this.dgvScrapReason.IsSetInputColumnsColor = true;
- //权限控制
- FormPermissionManager.FormPermissionControl(this.Name, this,
- LogInUserInfo.CurrentUser.CurrentUserEntity.UserRightData, LogInUserInfo.CurrentUser.CurrentUserEntity.FunctionData);
- this.dgvScrapReason.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
- }
- catch (Exception ex)
- {
- // 对异常进行共通处理
- ExceptionManager.HandleEventException(this.ToString(),
- System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
- }
- }
- /// <summary>
- /// 根据是否选中停用数据,加载数据
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void chkDisplayDisabledData_CheckedChanged(object sender, EventArgs e)
- {
- try
- {
- this.RefreshData();
- this.SetSaveBtnStatus();
- this.dgvScrapReason.IsSetInputColumnsColor = true;
- }
- catch (Exception ex)
- {
- // 对异常进行共通处理
- ExceptionManager.HandleEventException(this.ToString(),
- System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
- }
- }
- /// <summary>
- /// 窗体关闭事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void F_PM_0906_FormClosed(object sender, FormClosedEventArgs e)
- {
- _instance = null;
- }
- #endregion
- #region 私有方法
- /// <summary>
- /// 根据取得的数据字典数据,生成树形结构
- /// </summary>
- private void CreateScrapTypeTree()
- {
- try
- {
- if (this.tvwScrapType.Nodes.Count > Constant.INT_IS_ZERO)
- {
- // 添加节点前,保存状态
- SaveTreeNodesStatus(this.tvwScrapType.Nodes);
- }
- // 将所有节点清除
- this.tvwScrapType.Nodes.Clear();
- DataTable scrapTypeTable = new DataTable();
- scrapTypeTable.Columns.Add("ScrapType");
- scrapTypeTable.Columns.Add("ScrapTypeName");
- DataRow row = scrapTypeTable.NewRow();
- row["ScrapType"] = "0";
- row["ScrapTypeName"] = "开模报损";
- scrapTypeTable.Rows.Add(row);
- row = scrapTypeTable.NewRow();
- row["ScrapType"] = "1";
- row["ScrapTypeName"] = "普通报损";
- scrapTypeTable.Rows.Add(row);
- //生成树形结构
- foreach (DataRow newRootRow in scrapTypeTable.Rows)
- {
- TreeNode rootNode = new TreeNode();
- rootNode.Name = newRootRow["ScrapType"] + "";
- rootNode.Text = newRootRow["ScrapTypeName"] + "";
- rootNode.Tag = newRootRow["ScrapType"] + "";
- this.tvwScrapType.Nodes.Add(rootNode);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 获取报损原因数据
- /// </summary>
- private void GetScrapTypeData()
- {
- try
- {
- // 取得报损原因
- ClientRequestEntity cre = new ClientRequestEntity();
- cre.NameSpace = "MST_ScrapReason";
- cre.Name = "GetScrapReasonData";
- cre.Properties["ALL"] = "1";
- ServiceResultEntity result = new ServiceResultEntity();
- result = (ServiceResultEntity)DoAsync(new BaseAsyncMethod(() => { return SystemModuleProxy.Service.DoBarCodePrint(cre); }));
- // 没有数据时,直接返回
- if (result == null || result.Data == null || result.Data.Tables.Count <= 0)
- {
- return;
- }
- _initDataSource = result.Data.Tables[0];
- _dtSourse = result.Data.Tables[0];
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 保存事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnSave_Click(object sender, EventArgs e)
- {
- try
- {
- SaveScrapReason();
- }
- catch (Exception ex)
- {
- this.btnSave.Enabled = true;
- this.btnCancel.Enabled = true;
- // 对异常进行共通处理
- ExceptionManager.HandleEventException(this.ToString(),
- System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
- }
- }
- /// <summary>
- /// 关闭按钮事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnCancel_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- /// <summary>
- /// 选择数据字典TreeView中的节点前判断,如果数据有改变提示保存
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void tvwDictionaryType_BeforeSelect(object sender, TreeViewCancelEventArgs e)
- {
- try
- {
- this.dgvScrapReason.AllowUserToAddRows = true;
- // 判断数据是否被修改过,修改过需要提示保存消息
- if (DataJudge.IsChange((DataTable)this.dgvScrapReason.DataSource))
- {
- DialogResult dialogResult = MessageBox.Show(Messages.MSG_CMN_Q001, this.Text,
- MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
- // 保存修改内容
- if (dialogResult == DialogResult.Yes)
- {
- SaveScrapReason();
- }
- else if (dialogResult == DialogResult.Cancel)
- {
- e.Cancel = true;
- }
- //选择否回滚
- else
- {
- ((DataTable)this.dgvScrapReason.DataSource).RejectChanges();
- }
- }
- }
- catch (Exception ex)
- {
- // 对异常进行共通处理
- ExceptionManager.HandleEventException(this.ToString(),
- System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
- }
- }
- /// <summary>
- /// 选择树中任意值,右边数据跟着变动
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void tvwDictionaryType_AfterSelect(object sender, TreeViewEventArgs e)
- {
- try
- {
- this.lblDictionaryType.Text = "当前选择的字典类型:" + e.Node.Text;
- //判断父节点是否存在,表格联动
- TreeNode node = this.tvwScrapType.SelectedNode;
- if (node != null)
- {
- this.dgvScrapReason.ReadOnly = false;
- this._dtSourse.AcceptChanges();
- this.RefreshData();
- }
- else
- {
- this.dgvScrapReason.DataSource = null;
- this.dgvScrapReason.ReadOnly = true;
- this.dgvScrapReason.AllowUserToAddRows = false;
- }
- }
- catch (Exception ex)
- {
- // 对异常进行共通处理
- ExceptionManager.HandleEventException(this.ToString(),
- System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
- }
- }
- /// <summary>
- /// 选中新行时,设置默认值
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dgvScrapReason_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
- {
- // 设置有效属性为选中状态
- e.Row.Cells["ValueFlag"].Value = Constant.INT_IS_ONE;
- // 增加一行时,给类型赋值
- e.Row.Cells["ScrapType"].Value = this.tvwScrapType.SelectedNode.Tag + "";
- e.Row.Cells["DisplayNo"].Value = e.Row.Index + 1;
- }
- /// <summary>
- /// 数据字典信息输入格式控制
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dgvScrapReason_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
- {
- try
- {
- // 只对文本框做控制
- if (e.Control is DataGridViewTextBoxEditingControl)
- {
- DataGridViewTextBoxEditingControl EditingControl = (DataGridViewTextBoxEditingControl)e.Control;
- EditingControl.KeyPress += new KeyPressEventHandler(dgvScrapReason_KeyPress);
- }
- }
- catch (Exception ex)
- {
- // 对异常进行共通处理
- ExceptionManager.HandleEventException(this.ToString(),
- System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
- }
- }
- /// <summary>
- /// 屏蔽除了数字以外的输入(显示顺序)
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dgvScrapReason_KeyPress(object sender, KeyPressEventArgs e)
- {
- // 显示顺序列只允许输入数字和回格
- if ("DisplayNo".Equals(this.dgvScrapReason.Columns[this
- .dgvScrapReason.CurrentCell.ColumnIndex].Name))
- {
- if (e.KeyChar != '\b' && !char.IsDigit(e.KeyChar))
- {
- e.Handled = true;
- }
- }
- }
- /// <summary>
- /// 检查是否有重复的字典名称以及将为输入顺序设置成0
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dgvScrapReason_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
- {
- try
- {
- if (!dgvScrapReason.CurrentRow.IsNewRow)
- {
- DataGridViewRow rowItem = dgvScrapReason.Rows[e.RowIndex];
- DataGridViewColumn columnItem = dgvScrapReason.Columns[e.ColumnIndex];
- object columnvalue = rowItem.Cells[columnItem.Name].EditedFormattedValue;
- // 破损原因
- if ("ScrapReason".Equals(columnItem.Name))
- {
- if (columnvalue != null || !string.IsNullOrEmpty(columnvalue.ToString().Trim()))
- {
- if (!Utility.IsUnique(columnvalue.ToString(),
- dgvScrapReason.CurrentRow.Index, this.dgvScrapReason, "ScrapReason"))
- {
- // 单元格的错误消息
- this.dgvScrapReason.CurrentRow.ErrorText = string.Format(
- Messages.MSG_CMN_W006, "报损原因");
- e.Cancel = true;
- this.btnSave.Enabled = false;
- return;
- }
- }
- }
- // 清除单元格的错误消息
- this.dgvScrapReason.CurrentRow.ErrorText = string.Empty;
- this.dgvScrapReason.IsSetInputColumnsColor = true;
- // 显示顺序为空时设置为0
- if ("DisplayNo".Equals(columnItem.Name)
- && string.IsNullOrEmpty(columnvalue.ToString()))
- {
- dgvScrapReason.CurrentCell.Value = Constant.INT_IS_ZERO;
- dgvScrapReason.RefreshEdit();
- }
- }
- }
- catch (Exception ex)
- {
- // 对异常进行共通处理
- ExceptionManager.HandleEventException(this.ToString(),
- System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
- }
- }
- /// <summary>
- /// 单元格校验:根据数据是否变化,设置保存按钮的可用状态
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dgvScrapReason_CellValidated(object sender, DataGridViewCellEventArgs e)
- {
- try
- {
- this.SetSaveBtnStatus();
- }
- catch (Exception ex)
- {
- // 对异常进行共通处理
- ExceptionManager.HandleEventException(this.ToString(),
- System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
- }
- }
- /// <summary>
- /// 行校验,不能为空
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dgvScrapReason_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
- {
- try
- {
- // 按Esc键时不校验
- if (!dgvScrapReason.IsCurrentRowDirty)
- {
- this.dgvScrapReason.IsSetInputColumnsColor = true;
- return;
- }
- DataGridViewRow row = dgvScrapReason.CurrentRow;
- if (!row.IsNewRow)
- {
- // 报损原因不能为空
- if (row.Cells["ScrapReason"].Value == null
- || string.IsNullOrEmpty(row.Cells["ScrapReason"].Value.ToString().Trim()))
- {
- // 单元格的错误消息
- this.dgvScrapReason.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.dgvScrapReason.CurrentRow.ErrorText = string.Format(Messages.MSG_CMN_W005, "显示顺序");
- e.Cancel = true;
- this.btnSave.Enabled = false;
- return;
- }
- // 过滤掉数据字典名称,备注首尾的空格
- row.Cells["ScrapReason"].Value = row.Cells["ScrapReason"].Value.ToString().Trim();
- row.Cells["Remarks"].Value = row.Cells["Remarks"].Value == null ? "" : row.Cells["Remarks"].Value.ToString().Trim();
- // 清除单元格的错误消息
- this.dgvScrapReason.CurrentRow.ErrorText = string.Empty;
- }
- }
- catch (Exception ex)
- {
- // 对异常进行共通处理
- ExceptionManager.HandleEventException(this.ToString(),
- System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
- }
- }
- /// <summary>
- /// 行校验:根据数据是否变化,设置保存按钮的可用状态
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dgvScrapReason_RowValidated(object sender, DataGridViewCellEventArgs e)
- {
- try
- {
- this.SetSaveBtnStatus();
- }
- catch (Exception ex)
- {
- // 对异常进行共通处理
- ExceptionManager.HandleEventException(this.ToString(),
- System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
- }
- }
- /// <summary>
- /// 刷新数据
- /// </summary>
- private object RefreshData()
- {
- try
- {
- // 获取相应节点数据
- this._scrapType = this.tvwScrapType.SelectedNode.Tag.ToString();
- // 获取是否查询正常数据的标识
- this._valueFalg = Convert.ToByte(this.chkDisplayDisabledData.Checked);
- // 获取查询的数据集
- StringBuilder sbFilter = new StringBuilder();
- sbFilter.Append(" 1=1");
- if (this._valueFalg == Constant.INT_IS_ZERO)
- {
- sbFilter.Append(" and ValueFlag = 1");
- }
- if (this._scrapType != "")
- {
- sbFilter.Append(" and ScrapType = '" + _scrapType + "'");
- }
- this._dtSourse.DefaultView.RowFilter = sbFilter.ToString();
- // 选中新行时,自动显示顺序
- this._dtSourse.TableNewRow += new DataTableNewRowEventHandler(Table_SelectNewRow);
- this.dgvScrapReason.DataSource = this._dtSourse;
- this.dgvScrapReason.IsSetInputColumnsColor = true;
- return Constant.INT_IS_ZERO;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 保存的共同方法
- /// </summary>
- private void SaveScrapReason()
- {
- try
- {
- // 异步处理
- ClientRequestEntity cre = new ClientRequestEntity();
- cre.NameSpace = "MST_ScrapReason";
- cre.Name = "SaveScrapReasonData";
- DataTable returnTable = (DataTable)this.dgvScrapReason.DataSource;
- cre.Data = new DataSet();
- cre.Data.Tables.Add(returnTable.Copy());
- ServiceResultEntity result = new ServiceResultEntity();
- result = (ServiceResultEntity)DoAsync(new BaseAsyncMethod(() => { return SystemModuleProxy.Service.DoBarCodePrint(cre); }));
- int resultRow = (int)result.Result;
- // 如果保存出错,不关闭窗体
- if (resultRow > 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
- {
- this.btnSave.Enabled = false;
- MessageBox.Show(string.Format(Messages.MSG_CMN_W001, "报损原因", "保存"),
- this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 增加一行,自动计算显示顺序
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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["ScrapType"] = this.tvwScrapType.SelectedNode.Tag.ToString();
- }
- }
- /// <summary>
- /// 保存数据字典TreeView的状态
- /// </summary>
- /// <param name="nodes"></param>
- 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;
- }
- }
- /// <summary>
- /// 还原保存前产品类型TreeView的状态
- /// </summary>
- /// <param name="nodes">节点</param>
- private void SetTreeNodesStatus(TreeNodeCollection nodes)
- {
- try
- {
- foreach (TreeNode node in nodes)
- {
- if (this.NodesStatus[node.FullPath] != null)
- {
- // 展开树节点
- node.Expand();
- }
- // 节点路径和前选中的路径相同 设置节点为选中状态
- if (node.FullPath == SelectNodeFullPath)
- {
- this.tvwScrapType.SelectedNode = node;
- }
- SetTreeNodesStatus(node.Nodes);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 设置保存按钮的可用状态
- /// </summary>
- private void SetSaveBtnStatus()
- {
- if (DataJudge.IsChange((DataTable)this.dgvScrapReason.DataSource))
- {
- this.btnSave.Enabled = true;
- }
- else
- {
- this.btnSave.Enabled = false;
- }
- }
- #endregion
- }
- }
|