F_MST_0413.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*******************************************************************************
  2. * Copyright(c) 2014 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:F_MST_0413.cs
  5. * 2.功能描述:PLC连接参数
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2017/11/14 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Data;
  12. using System.Windows.Forms;
  13. using Dongke.IBOSS.PRD.Basics.BaseResources;
  14. using Dongke.IBOSS.PRD.Basics.BaseControls;
  15. using Dongke.IBOSS.PRD.Client.CommonModule;
  16. using Dongke.IBOSS.PRD.Basics.Library;
  17. using Dongke.IBOSS.PRD.WCF.Proxys;
  18. using Dongke.IBOSS.PRD.Client.DataModels;
  19. using Dongke.IBOSS.PRD.WCF.DataModels;
  20. namespace Dongke.IBOSS.PRD.Client.SystemModule
  21. {
  22. public partial class F_MST_0413 : FormBase
  23. {
  24. #region 成员变量
  25. // 窗体的单例模式
  26. private static F_MST_0413 _instance;
  27. // 条码打印机数据源
  28. private DataTable _dtSourse;
  29. #endregion
  30. #region 构造函数
  31. /// <summary>
  32. /// 构造函数
  33. /// </summary>
  34. public F_MST_0413()
  35. {
  36. InitializeComponent();
  37. // 窗口标题
  38. //this.Text = FormTitles.F_MST_0411;
  39. // 按钮
  40. this.btnSave.Text = ButtonText.BTN_SAVE;
  41. this.btnCancel.Text = ButtonText.BTN_CLOSE;
  42. }
  43. #endregion
  44. #region 单例模式
  45. /// <summary>
  46. /// 单例模式,防止重复创建窗体
  47. /// </summary>
  48. public static F_MST_0413 Instance
  49. {
  50. get
  51. {
  52. if (_instance == null)
  53. {
  54. _instance = new F_MST_0413();
  55. }
  56. return _instance;
  57. }
  58. }
  59. #endregion
  60. #region 事件
  61. /// <summary>
  62. /// 窗体加载
  63. /// </summary>
  64. /// <param name="sender"></param>
  65. /// <param name="e"></param>
  66. private void F_MST_0411_Load(object sender, System.EventArgs e)
  67. {
  68. try
  69. {
  70. // 设置datagridview不自动创建列
  71. this.dgvPrinter.AutoGenerateColumns = false;
  72. // 加载打印机数据
  73. ClientRequestEntity cre = new ClientRequestEntity();
  74. cre.NameSpace = "MST0413";
  75. cre.Name = "GetAllPLC";
  76. ServiceResultEntity sre = CommonModuleProxy.Service.DoRequest(cre);
  77. this._dtSourse = sre.Data.Tables[0];
  78. this.RefreshDataGridViewData();
  79. this.dgvPrinter.IsSetInputColumnsColor = true;
  80. //权限控制
  81. //FormPermissionManager.FormPermissionControl(this.Name, this,
  82. // LogInUserInfo.CurrentUser.CurrentUserEntity.UserRightData,
  83. // LogInUserInfo.CurrentUser.CurrentUserEntity.FunctionData);
  84. this.dgvPrinter.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
  85. }
  86. catch (Exception ex)
  87. {
  88. // 对异常进行共通处理
  89. ExceptionManager.HandleEventException(this.ToString(),
  90. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  91. }
  92. }
  93. /// <summary>
  94. /// 点击右上角关闭按钮
  95. /// </summary>
  96. /// <param name="sender"></param>
  97. /// <param name="e"></param>
  98. private void F_MST_0411_FormClosing(object sender, FormClosingEventArgs e)
  99. {
  100. try
  101. {
  102. // 关闭的时候需要判断是否有数据变化
  103. if (DataJudge.IsChange((DataTable)this.dgvPrinter.DataSource))
  104. {
  105. DialogResult dialogResult = MessageBox.Show(Messages.MSG_CMN_Q001, this.Text,
  106. MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
  107. // 保存改变的数据
  108. if (dialogResult == DialogResult.Yes)
  109. {
  110. if (Conservation() == -1)
  111. {
  112. e.Cancel = true;
  113. return;
  114. }
  115. // 异步处理
  116. object result = DoAsync(new BaseAsyncMethod(SaveData));
  117. // 保存成功,不关闭窗体
  118. if (Convert.ToInt32(result) > Constant.INT_IS_ZERO)
  119. {
  120. e.Cancel = true;
  121. MessageBox.Show(string.Format(Messages.MSG_CMN_I001, this.Text, "保存"),
  122. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  123. this.btnSave.Enabled = false;
  124. // 加载产品缺陷管理数据
  125. this._dtSourse.AcceptChanges();
  126. }
  127. else if (Convert.ToInt32(result) == -1)
  128. {
  129. e.Cancel = true;
  130. MessageBox.Show(string.Format(Messages.MSG_CMN_W006, "PLC名"),
  131. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  132. }
  133. else
  134. {
  135. e.Cancel = true;
  136. MessageBox.Show(string.Format(Messages.MSG_CMN_W001, this.Text, "保存"),
  137. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  138. }
  139. }
  140. else if (dialogResult == DialogResult.Cancel)
  141. {
  142. e.Cancel = true;
  143. }
  144. }
  145. }
  146. catch (Exception ex)
  147. {
  148. // 对异常进行共通处理
  149. ExceptionManager.HandleEventException(this.ToString(),
  150. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  151. }
  152. }
  153. /// <summary>
  154. /// 窗体关闭后,释放单例资源
  155. /// </summary>
  156. /// <param name="sender"></param>
  157. /// <param name="e"></param>
  158. private void F_MST_0411_FormClosed(object sender, FormClosedEventArgs e)
  159. {
  160. _instance = null;
  161. }
  162. /// <summary>
  163. /// 选中新行时,设置默认值
  164. /// </summary>
  165. /// <param name="sender"></param>
  166. /// <param name="e"></param>
  167. private void dgvDataDefect_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
  168. {
  169. // 设置有效属性为选中状态
  170. e.Row.Cells["ValueFlag"].Value = Constant.INT_IS_ONE;
  171. e.Row.Cells["DisplayNo"].Value = e.Row.Index + 1;
  172. }
  173. /// <summary>
  174. /// 产品缺陷管理信息输入格式控制
  175. /// </summary>
  176. /// <param name="sender"></param>
  177. /// <param name="e"></param>
  178. private void dgvDataDefect_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  179. {
  180. try
  181. {
  182. // 只对文本框做控制
  183. if (e.Control is DataGridViewTextBoxEditingControl)
  184. {
  185. DataGridViewTextBoxEditingControl EditingControl = (DataGridViewTextBoxEditingControl)e.Control;
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. // 对异常进行共通处理
  191. ExceptionManager.HandleEventException(this.ToString(),
  192. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  193. }
  194. }
  195. /// <summary>
  196. /// 单元格验证,检查
  197. /// </summary>
  198. /// <param name="sender"></param>
  199. /// <param name="e"></param>
  200. private void dgvDataDefect_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
  201. {
  202. try
  203. {
  204. if (this.ActiveControl != null && "btnClose".Equals(this.ActiveControl.Name))
  205. {
  206. return;
  207. }
  208. if (!dgvPrinter.CurrentRow.IsNewRow)
  209. {
  210. DataGridViewRow rowItem = dgvPrinter.Rows[e.RowIndex];
  211. DataGridViewColumn columnItem = dgvPrinter.Columns[e.ColumnIndex];
  212. object columnvalue = rowItem.Cells[columnItem.Name].EditedFormattedValue;
  213. // 打印机名
  214. if ("PLCName".Equals(columnItem.Name))
  215. {
  216. if (columnvalue != null && !string.IsNullOrEmpty(columnvalue + string.Empty))
  217. {
  218. if (!Utility.IsUnique(columnvalue.ToString(),
  219. dgvPrinter.CurrentRow.Index, this.dgvPrinter, "PLCName"))
  220. {
  221. // 单元格的错误消息
  222. this.dgvPrinter.CurrentRow.ErrorText = string.Format(
  223. Messages.MSG_CMN_W006, "PLC名");
  224. e.Cancel = true;
  225. this.btnSave.Enabled = false;
  226. return;
  227. }
  228. }
  229. }
  230. // 清除单元格的错误消息
  231. this.dgvPrinter.CurrentRow.ErrorText = string.Empty;
  232. }
  233. }
  234. catch (Exception ex)
  235. {
  236. // 对异常进行共通处理
  237. ExceptionManager.HandleEventException(this.ToString(),
  238. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  239. }
  240. }
  241. /// <summary>
  242. /// 根据数据是否变化,设置保存按钮的可用状态
  243. /// </summary>
  244. /// <param name="sender"></param>
  245. /// <param name="e"></param>
  246. private void dgvDataDefect_CellValidated(object sender, DataGridViewCellEventArgs e)
  247. {
  248. try
  249. {
  250. this.SetSaveBtnStatus();
  251. }
  252. catch (Exception ex)
  253. {
  254. // 对异常进行共通处理
  255. ExceptionManager.HandleEventException(this.ToString(),
  256. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  257. }
  258. }
  259. /// <summary>
  260. /// 行校验
  261. /// </summary>
  262. /// <param name="sender"></param>
  263. /// <param name="e"></param>
  264. private void dgvDataDefect_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
  265. {
  266. try
  267. {
  268. // 按Esc键时不校验
  269. if (!dgvPrinter.IsCurrentRowDirty)
  270. {
  271. this.dgvPrinter.IsSetInputColumnsColor = true;
  272. return;
  273. }
  274. if (this.ActiveControl != null && "btnClose".Equals(this.ActiveControl.Name))
  275. {
  276. // 清除单元格的错误消息
  277. this.dgvPrinter.CurrentRow.ErrorText = string.Empty;
  278. return;
  279. }
  280. DataGridViewRow row = dgvPrinter.CurrentRow;
  281. if (!row.IsNewRow)
  282. {
  283. // 判断打印机名不能为空
  284. if (row.Cells["PLCName"].Value == null
  285. || string.IsNullOrEmpty(row.Cells["PLCName"].Value + string.Empty))
  286. {
  287. // 单元格的错误消息
  288. this.dgvPrinter.CurrentRow.ErrorText = string.Format(Messages.MSG_CMN_W005, "PLC名");
  289. e.Cancel = true;
  290. this.btnSave.Enabled = false;
  291. return;
  292. }
  293. if (row.Cells["IP"].Value == null
  294. || string.IsNullOrEmpty(row.Cells["IP"].Value + string.Empty))
  295. {
  296. // 单元格的错误消息
  297. this.dgvPrinter.CurrentRow.ErrorText = string.Format(Messages.MSG_CMN_W005, "IP");
  298. e.Cancel = true;
  299. this.btnSave.Enabled = false;
  300. return;
  301. }
  302. if (row.Cells["Port"].Value == null
  303. || string.IsNullOrEmpty(row.Cells["Port"].Value + string.Empty))
  304. {
  305. // 单元格的错误消息
  306. this.dgvPrinter.CurrentRow.ErrorText = string.Format(Messages.MSG_CMN_W005, "端口");
  307. e.Cancel = true;
  308. this.btnSave.Enabled = false;
  309. return;
  310. }
  311. if (row.Cells["dec"].Value == null
  312. || string.IsNullOrEmpty(row.Cells["dec"].Value + string.Empty))
  313. {
  314. // 单元格的错误消息
  315. this.dgvPrinter.CurrentRow.ErrorText = string.Format(Messages.MSG_CMN_W005, "软元件描述");
  316. e.Cancel = true;
  317. this.btnSave.Enabled = false;
  318. return;
  319. }
  320. // 清除单元格的错误消息
  321. this.dgvPrinter.CurrentRow.ErrorText = string.Empty;
  322. }
  323. }
  324. catch (Exception ex)
  325. {
  326. // 对异常进行共通处理
  327. ExceptionManager.HandleEventException(this.ToString(),
  328. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  329. }
  330. }
  331. /// <summary>
  332. /// 根据数据是否变化,设置保存按钮的可用状态
  333. /// </summary>
  334. /// <param name="sender"></param>
  335. /// <param name="e"></param>
  336. private void dgvDataDefect_RowValidated(object sender, DataGridViewCellEventArgs e)
  337. {
  338. try
  339. {
  340. this.SetSaveBtnStatus();
  341. }
  342. catch (Exception ex)
  343. {
  344. // 对异常进行共通处理
  345. ExceptionManager.HandleEventException(this.ToString(),
  346. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  347. }
  348. }
  349. /// <summary>
  350. /// 根据是否选中停用数据,加载数据
  351. /// </summary>
  352. /// <param name="sender"></param>
  353. /// <param name="e"></param>
  354. private void chkDisplayDisabledData_CheckedChanged(object sender, EventArgs e)
  355. {
  356. try
  357. {
  358. this.RefreshDataGridViewData();
  359. this.SetSaveBtnStatus();
  360. this.dgvPrinter.IsSetInputColumnsColor = true;
  361. }
  362. catch (Exception ex)
  363. {
  364. // 对异常进行共通处理
  365. ExceptionManager.HandleEventException(this.ToString(),
  366. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  367. }
  368. }
  369. /// <summary>
  370. /// 单元格输入时,给自动生成的行设置颜色
  371. /// </summary>
  372. /// <param name="sender"></param>
  373. /// <param name="e"></param>
  374. private void dgvDataDefect_UserAddedRow(object sender, DataGridViewRowEventArgs e)
  375. {
  376. this.dgvPrinter.IsSetInputColumnsColor = true;
  377. }
  378. /// <summary>
  379. /// 设置排序时列表的颜色
  380. /// </summary>
  381. private void dgvDataDefect_Sorted(object sender, EventArgs e)
  382. {
  383. this.dgvPrinter.IsSetInputColumnsColor = true;
  384. }
  385. /// <summary>
  386. /// 保存
  387. /// </summary>
  388. /// <param name="sender"></param>
  389. /// <param name="e"></param>
  390. private void btnSave_Click(object sender, EventArgs e)
  391. {
  392. try
  393. {
  394. int results = Conservation();
  395. //判断单元格必输项不能为空
  396. if (results == -1)
  397. {
  398. return;
  399. }
  400. // 异步处理
  401. this.btnSave.Enabled = false;
  402. this.btnCancel.Enabled = false;
  403. int result = (int)DoAsync(new BaseAsyncMethod(SaveData));
  404. this.btnSave.Enabled = true;
  405. this.btnCancel.Enabled = true;
  406. // 缺陷类别数据保存成功
  407. if (result == 1)
  408. {
  409. MessageBox.Show(string.Format(Messages.MSG_CMN_I001, this.Text, "保存"),
  410. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  411. // 加载缺陷类别管理数据
  412. this._dtSourse.AcceptChanges();
  413. this.btnSave.Enabled = false;
  414. }
  415. else if (result == -1)
  416. {
  417. MessageBox.Show(string.Format(Messages.MSG_CMN_W006, "PLC名"),
  418. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  419. }
  420. else
  421. {
  422. MessageBox.Show(string.Format(Messages.MSG_CMN_W001, this.Text, "保存"),
  423. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  424. }
  425. this.dgvPrinter.IsSetInputColumnsColor = true;
  426. }
  427. catch (Exception ex)
  428. {
  429. this.btnSave.Enabled = true;
  430. this.btnCancel.Enabled = true;
  431. // 对异常进行共通处理
  432. ExceptionManager.HandleEventException(this.ToString(),
  433. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  434. }
  435. }
  436. /// <summary>
  437. /// 点击关闭按钮
  438. /// </summary>
  439. /// <param name="sender"></param>
  440. /// <param name="e"></param>
  441. private void btnCancel_Click(object sender, EventArgs e)
  442. {
  443. this.Close();
  444. }
  445. /// <summary>
  446. /// 添加新行
  447. /// </summary>
  448. /// <param name="sender"></param>
  449. /// <param name="e"></param>
  450. private void dgvPrinter_NewRowNeeded(object sender, DataGridViewRowEventArgs e)
  451. {
  452. try
  453. {
  454. //DataTable dt =(dgvPrinter.DataSource as DataTable);
  455. //if(dt == null || dt.Rows.Count == 0)
  456. //{
  457. // e.Row.Cells["DispalyNo"].Value = 1;
  458. //}
  459. //DataRow[] drs = dt.Select("valueflag = 1","DispalyNo desc");
  460. //if(drs == null || drs.Length == 0)
  461. //{
  462. // e.Row.Cells["DispalyNo"].Value = 1;
  463. //}
  464. //else
  465. //{
  466. // e.Row.Cells["DispalyNo"].Value = Convert.ToInt32(drs[0]) + 1;
  467. //}
  468. e.Row.Cells["DispalyNo"].Value = e.Row.Index + 1;
  469. }
  470. catch (Exception ex)
  471. {
  472. // 对异常进行共通处理
  473. ExceptionManager.HandleEventException(this.ToString(),
  474. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  475. }
  476. }
  477. #endregion
  478. #region 私有方法
  479. /// <summary>
  480. /// 得到显示数据
  481. /// </summary>
  482. /// <returns></returns>
  483. private void RefreshDataGridViewData()
  484. {
  485. try
  486. {
  487. if (this.chkDisplayDisabledData.Checked == false)
  488. {
  489. this._dtSourse.DefaultView.RowFilter = "ValueFlag=1";
  490. }
  491. else
  492. {
  493. this._dtSourse.DefaultView.RowFilter = null;
  494. }
  495. this.dgvPrinter.DataSource = _dtSourse;
  496. this.dgvPrinter.AutoResizeColumns();
  497. this.btnSave.Enabled = false;
  498. }
  499. catch (Exception ex)
  500. {
  501. throw ex;
  502. }
  503. }
  504. /// <summary>
  505. /// 保存数据
  506. /// </summary>
  507. /// <returns>是否成功</returns>
  508. private object SaveData()
  509. {
  510. try
  511. {
  512. // 获取当前数据源
  513. ClientRequestEntity cre = new ClientRequestEntity();
  514. cre.NameSpace = "MST0413";
  515. cre.Name = "SetPLC";
  516. cre.Data = this._dtSourse.DataSet;
  517. ServiceResultEntity sre = CommonModuleProxy.Service.DoRequest(cre);
  518. if (sre.Status == Constant.ServiceResultStatus.Success)
  519. {
  520. return 1;
  521. }
  522. else if (sre.Status == Constant.ServiceResultStatus.DataDuplicated)
  523. {
  524. return -1;
  525. }
  526. else
  527. {
  528. return 0;
  529. }
  530. }
  531. catch (Exception ex)
  532. {
  533. throw ex;
  534. }
  535. }
  536. /// <summary>
  537. /// 设置保存按钮的可用状态
  538. /// </summary>
  539. private void SetSaveBtnStatus()
  540. {
  541. if (DataJudge.IsChange((DataTable)this.dgvPrinter.DataSource))
  542. {
  543. this.btnSave.Enabled = true;
  544. }
  545. else
  546. {
  547. this.btnSave.Enabled = false;
  548. }
  549. }
  550. /// <summary>
  551. /// 保存时单元格必输项不能为空
  552. /// </summary>
  553. private int Conservation()
  554. {
  555. DataTable dataDefectData = (DataTable)this.dgvPrinter.DataSource;
  556. DataRow[] d = dataDefectData.Select("PLCName is null or PLCName = '' or IP is null or IP = '' or Port is null or PLCDescription is null or PLCDescription = ''");
  557. if (d != null && d.Length > 0)
  558. {
  559. MessageBox.Show(string.Format(Messages.MSG_CMN_W005, "PLC名、IP、端口、描述"),
  560. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  561. this.btnSave.Enabled = false;
  562. return -1;
  563. }
  564. return 0;
  565. }
  566. #endregion
  567. }
  568. }