CNAS取数仪器端升级
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

frmCNASValue.cs 22KB

5 anni fa
5 anni fa
5 anni fa
5 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using CnasSynchronusClient;
  10. using CnasSynchrousModel;
  11. namespace CNAS_DBSync
  12. {
  13. public partial class frmCNASValue : Form
  14. {
  15. public SyncInstrumentItemInfo syncInstrument;
  16. private ComboBox cbxConditionValue;
  17. private List<string> lstConditionValueType = new List<string>() { "等于", "连接", "若...则...", "截取", "除以", "乘以", "小数位数","数值相加(减)" };
  18. public frmCNASValue(SyncInstrumentItemInfo syncInstrument)
  19. {
  20. InitializeComponent();
  21. this.syncInstrument = syncInstrument;
  22. }
  23. private void frmCNASValue_Load(object sender, EventArgs e)
  24. {
  25. if (syncInstrument == null || syncInstrument.LstSyncPramas == null|| syncInstrument.LstSyncPramas.Count==0)
  26. {
  27. MessageBox.Show("请先指定至少一个映射字段。");
  28. return;
  29. }
  30. //初始化ComBobox
  31. InnitalComboBox();
  32. //加载数据
  33. LoadShowData();
  34. }
  35. private void InnitalComboBox()
  36. {
  37. cbxConditionValue = new ComboBox();
  38. cbxConditionValue.SelectedIndexChanged += new EventHandler(cbxConditionValue_SelectedIndexChanged);
  39. cbxConditionValue.SelectionChangeCommitted += new EventHandler(cbxConditionValue_SelectionChangedCommitted);
  40. cbxConditionValue.Visible = false;
  41. cbxConditionValue.DataSource = lstConditionValueType;
  42. }
  43. private void cbxConditionValue_SelectionChangedCommitted(object sender, EventArgs e)
  44. {
  45. cbxConditionValue.Visible = false;
  46. }
  47. private void cbxConditionValue_SelectedIndexChanged(object sender, EventArgs e)
  48. {
  49. //当前选中单元格的值将随之发生更改
  50. if (dgvCnas.CurrentCell == null) return;
  51. dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells["condition"].Value = cbxConditionValue.Text;
  52. }
  53. private void LoadShowData()
  54. {
  55. //1.根据映射表中的表名,获取该表所有字段,全部显示
  56. string strTableName = syncInstrument.LstSyncPramas[0].TargetTable;
  57. DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTablesStruct(strTableName, syncInstrument.SyncTargetDBInfo);
  58. if (dtTableStruct != null)
  59. {
  60. DataTable dtCnasShow = new DataTable();
  61. dtCnasShow.Columns.Add("CnasTableName");
  62. dtCnasShow.Columns.Add("CnasFieldName");
  63. dtCnasShow.Columns.Add("CnasDataType");
  64. foreach (DataColumn dc in dtTableStruct.Columns)
  65. {
  66. if (dc.ColumnName.ToUpper() == "ID") continue;
  67. dtCnasShow.Rows.Add(new object[] { strTableName,dc.ColumnName, dc.DataType });
  68. }
  69. dgvCnas.DataSource = dtCnasShow;
  70. }
  71. //2.根据已存储数据,匹配后填充到datagridview中
  72. if (syncInstrument.lstFixedValue == null)
  73. syncInstrument.lstFixedValue = new List<CnasConditionMapValue>();
  74. if (syncInstrument.lstFixedValue.Count == 0) return;
  75. foreach (DataGridViewRow dgvRow in dgvCnas.Rows)
  76. {
  77. string strCurrentTable = dgvRow.Cells["TableName"].Value.ToString();
  78. string strCurrentColumn = dgvRow.Cells["TableColumn"].Value.ToString();
  79. if (strCurrentTable == "" || strCurrentColumn == "") continue;
  80. var query=syncInstrument.lstFixedValue.Where(s => s.TableName == strCurrentTable && s.ColumnName == strCurrentColumn).ToList<CnasConditionMapValue>();
  81. if (query.Count == 1)
  82. {
  83. dgvRow.Cells["Value"].Value = query[0].Value;
  84. dgvRow.Cells["Condition"].Value = GiveShowByCondition(query[0]);
  85. }
  86. }
  87. }
  88. private void btnOK_Click(object sender, EventArgs e)
  89. {
  90. this.DialogResult = DialogResult.OK;
  91. this.Close();
  92. }
  93. private void dgvCnas_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  94. {
  95. if (e.ColumnIndex == 1) //此时是“值”列
  96. {
  97. //如果没有指定条件,无法存储结果
  98. if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value == null) return;
  99. //当前列名
  100. string strCurrentTable = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString();
  101. string strCurrentColumn = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString();
  102. string strCurrentType = dgvCnas.Rows[e.RowIndex].Cells["DataType"].Value.ToString();
  103. string strCurrentCondition= dgvCnas.Rows[e.RowIndex].Cells["condition"].Value.ToString();
  104. if (dgvCnas.CurrentCell.Value == null)
  105. {
  106. syncInstrument.lstFixedValue.RemoveAll(s => s.TableName == strCurrentTable && s.ColumnName == strCurrentColumn);
  107. return;
  108. }
  109. string strInputValue = dgvCnas.CurrentCell.Value.ToString();
  110. //检查合法性
  111. if (strCurrentColumn == "ID")
  112. {
  113. MessageBox.Show("该列不能指定固定值。");
  114. return;
  115. }
  116. bool bIfSuccess = true;
  117. switch (strCurrentType)
  118. {
  119. case "System.Decimal":
  120. decimal defaultdecimal = 0;
  121. if (!decimal.TryParse(strInputValue, out defaultdecimal))
  122. bIfSuccess = false;
  123. break;
  124. case "System.Int":
  125. int defaultint = 0;
  126. if (!int.TryParse(strInputValue, out defaultint))
  127. bIfSuccess = false;
  128. break;
  129. case "System.String":
  130. default:
  131. break;
  132. }
  133. if (!bIfSuccess)
  134. {
  135. MessageBox.Show("输入格式不正确,请重新输入。");
  136. return;
  137. }
  138. //将数据插入到数据源中
  139. var query = syncInstrument.lstFixedValue.Where(s => s.TableName == strCurrentTable && s.ColumnName == strCurrentColumn).ToList<CnasConditionMapValue>();
  140. if (query.Count >= 1)
  141. {
  142. GiveConditionByShow(query[0],strCurrentCondition);
  143. //query[0].Condition = strCurrentCondition == "Sub" ? MapCondition.Sub : (strCurrentCondition == "IFThen" ? MapCondition.IFThen: MapCondition.Equal);
  144. query[0].Value = strInputValue;
  145. }
  146. else
  147. {
  148. CnasConditionMapValue cnasFixed = new CnasConditionMapValue();
  149. cnasFixed.TableName = strCurrentTable;
  150. cnasFixed.ColumnName = strCurrentColumn;
  151. cnasFixed.Value = strInputValue;
  152. //cnasFixed.Condition= strCurrentCondition == "Sub" ? MapCondition.Sub : (strCurrentCondition == "IFThen" ? MapCondition.IFThen : MapCondition.Equal);
  153. GiveConditionByShow(cnasFixed, strCurrentCondition);
  154. syncInstrument.lstFixedValue.Add(cnasFixed);
  155. }
  156. }
  157. }
  158. public void GiveConditionByShow(CnasConditionMapValue cnasFixed,string strCurrentCondition)
  159. {
  160. switch (strCurrentCondition)
  161. {
  162. case "连接":
  163. cnasFixed.Condition = MapCondition.Sub;
  164. break;
  165. case "若...则...":
  166. cnasFixed.Condition = MapCondition.IFThen;
  167. break;
  168. case "截取":
  169. cnasFixed.Condition = MapCondition.SubString;
  170. break;
  171. case "除以":
  172. cnasFixed.Condition = MapCondition.Divided;
  173. break;
  174. case "乘以":
  175. cnasFixed.Condition = MapCondition.Multiplied;
  176. break;
  177. case "小数位数":
  178. cnasFixed.Condition = MapCondition.DecimalDigits;
  179. break;
  180. case "等于":
  181. cnasFixed.Condition = MapCondition.Equal;
  182. break;
  183. case "数值相加(减)":
  184. cnasFixed.Condition = MapCondition.AddSubtract;
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. public string GiveShowByCondition(CnasConditionMapValue cnasFixed)
  191. {
  192. string strCurrentCondition = "";
  193. switch (cnasFixed.Condition)
  194. {
  195. case MapCondition.Equal:
  196. strCurrentCondition = "等于";
  197. break;
  198. case MapCondition.Sub:
  199. strCurrentCondition = "连接";
  200. break;
  201. case MapCondition.IFThen:
  202. strCurrentCondition = "若...则...";
  203. break;
  204. case MapCondition.Divided:
  205. strCurrentCondition = "除以";
  206. break;
  207. case MapCondition.Multiplied:
  208. strCurrentCondition = "乘以";
  209. break;
  210. case MapCondition.SubString:
  211. strCurrentCondition = "截取";
  212. break;
  213. case MapCondition.DecimalDigits:
  214. strCurrentCondition = "小数位数";
  215. break;
  216. case MapCondition.AddSubtract:
  217. strCurrentCondition = "数值相加(减)";
  218. break;
  219. }
  220. return strCurrentCondition;
  221. }
  222. private void dgvCnas_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  223. {
  224. Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
  225. e.RowBounds.Location.Y,
  226. dgvCnas.RowHeadersWidth - 4,
  227. e.RowBounds.Height);
  228. TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
  229. dgvCnas.RowHeadersDefaultCellStyle.Font,
  230. rectangle,
  231. dgvCnas.RowHeadersDefaultCellStyle.ForeColor,
  232. TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
  233. }
  234. private void dgvCnas_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  235. {
  236. if (e.ColumnIndex == 0) //如果此时是“条件”列
  237. {
  238. ShowConditionValue();
  239. }
  240. if (e.ColumnIndex == 1)//如果此时是“值”列
  241. {
  242. if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value == null) return;
  243. if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value.ToString() == "连接")
  244. {
  245. CnasConditionMapValue cnasFixed = new CnasConditionMapValue
  246. {
  247. TableName = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString(),
  248. ColumnName = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString(),
  249. Condition = MapCondition.Sub
  250. };
  251. if (dgvCnas.Rows[e.RowIndex].Cells["Value"].Value!=null)
  252. cnasFixed.Value= dgvCnas.Rows[e.RowIndex].Cells["Value"].Value.ToString();
  253. frmConditionParam frmCondition = new frmConditionParam(syncInstrument, cnasFixed);
  254. if (frmCondition.ShowDialog() == DialogResult.OK)
  255. {
  256. if (frmCondition.conditionvalue.Value != null&& frmCondition.conditionvalue.Value.ToString()!="")
  257. {
  258. dgvCnas.Rows[e.RowIndex].Cells["Value"].Value = frmCondition.conditionvalue.Value;
  259. //将数据插入到数据源中
  260. var query = syncInstrument.lstFixedValue.Where(s => s.TableName == cnasFixed.TableName && s.ColumnName == cnasFixed.ColumnName).ToList<CnasConditionMapValue>();
  261. if (query.Count >= 1)
  262. {
  263. query[0].Condition = cnasFixed.Condition;
  264. query[0].Value = frmCondition.conditionvalue.Value;
  265. }
  266. else
  267. {
  268. cnasFixed.Value = frmCondition.conditionvalue.Value;
  269. syncInstrument.lstFixedValue.Add(cnasFixed);
  270. }
  271. }
  272. }
  273. }
  274. if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value.ToString() == "若...则...")
  275. {
  276. CnasConditionMapValue cnasFixed = new CnasConditionMapValue
  277. {
  278. TableName = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString(),
  279. ColumnName = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString(),
  280. Condition = MapCondition.IFThen
  281. };
  282. if (dgvCnas.Rows[e.RowIndex].Cells["Value"].Value != null)
  283. cnasFixed.Value = dgvCnas.Rows[e.RowIndex].Cells["Value"].Value.ToString();
  284. frmIfThenParams frmIfThen = new frmIfThenParams(syncInstrument, cnasFixed);
  285. if (frmIfThen.ShowDialog() == DialogResult.OK)
  286. {
  287. if (frmIfThen.conditionvalue.Value != null && frmIfThen.conditionvalue.Value.ToString() != "")
  288. {
  289. dgvCnas.Rows[e.RowIndex].Cells["Value"].Value = frmIfThen.conditionvalue.Value;
  290. //将数据插入到数据源中
  291. var query = syncInstrument.lstFixedValue.Where(s => s.TableName == cnasFixed.TableName && s.ColumnName == cnasFixed.ColumnName).ToList<CnasConditionMapValue>();
  292. if (query.Count >= 1)
  293. {
  294. query[0].Condition = cnasFixed.Condition;
  295. query[0].Value = frmIfThen.conditionvalue.Value;
  296. }
  297. else
  298. {
  299. cnasFixed.Value = frmIfThen.conditionvalue.Value;
  300. syncInstrument.lstFixedValue.Add(cnasFixed);
  301. }
  302. }
  303. }
  304. }
  305. if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value.ToString() == "截取")
  306. {
  307. CnasConditionMapValue cnasFixed = new CnasConditionMapValue
  308. {
  309. TableName = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString(),
  310. ColumnName = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString(),
  311. Condition = MapCondition.SubString
  312. };
  313. if (dgvCnas.Rows[e.RowIndex].Cells["Value"].Value != null)
  314. cnasFixed.Value = dgvCnas.Rows[e.RowIndex].Cells["Value"].Value.ToString();
  315. frmSplitParam frmSplitParam = new frmSplitParam(cnasFixed);
  316. if (frmSplitParam.ShowDialog() == DialogResult.OK)
  317. {
  318. if (frmSplitParam.conditionvalue.Value != null && frmSplitParam.conditionvalue.Value.ToString() != "")
  319. {
  320. dgvCnas.Rows[e.RowIndex].Cells["Value"].Value = frmSplitParam.conditionvalue.Value;
  321. //将数据插入到数据源中
  322. var query = syncInstrument.lstFixedValue.Where(s => s.TableName == cnasFixed.TableName && s.ColumnName == cnasFixed.ColumnName).ToList<CnasConditionMapValue>();
  323. if (query.Count >= 1)
  324. {
  325. query[0].Condition = cnasFixed.Condition;
  326. query[0].Value = frmSplitParam.conditionvalue.Value;
  327. }
  328. else
  329. {
  330. cnasFixed.Value = frmSplitParam.conditionvalue.Value;
  331. syncInstrument.lstFixedValue.Add(cnasFixed);
  332. }
  333. }
  334. }
  335. }
  336. if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value.ToString() == "数值相加(减)")
  337. {
  338. CnasConditionMapValue cnasFixed = new CnasConditionMapValue
  339. {
  340. TableName = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString(),
  341. ColumnName = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString(),
  342. Condition = MapCondition.AddSubtract
  343. };
  344. if (dgvCnas.Rows[e.RowIndex].Cells["Value"].Value != null)
  345. cnasFixed.Value = dgvCnas.Rows[e.RowIndex].Cells["Value"].Value.ToString();
  346. frmAddSubtract frmAddSubtract = new frmAddSubtract(syncInstrument, cnasFixed);
  347. if (frmAddSubtract.ShowDialog() == DialogResult.OK)
  348. {
  349. if (frmAddSubtract.conditionvalue.Value != null && frmAddSubtract.conditionvalue.Value.ToString() != "")
  350. {
  351. dgvCnas.Rows[e.RowIndex].Cells["Value"].Value = frmAddSubtract.conditionvalue.Value;
  352. //将数据插入到数据源中
  353. var query = syncInstrument.lstFixedValue.Where(s => s.TableName == cnasFixed.TableName && s.ColumnName == cnasFixed.ColumnName).ToList<CnasConditionMapValue>();
  354. if (query.Count >= 1)
  355. {
  356. query[0].Condition = cnasFixed.Condition;
  357. query[0].Value = frmAddSubtract.conditionvalue.Value;
  358. }
  359. else
  360. {
  361. cnasFixed.Value = frmAddSubtract.conditionvalue.Value;
  362. syncInstrument.lstFixedValue.Add(cnasFixed);
  363. }
  364. }
  365. }
  366. }
  367. dgvCnas.EndEdit();
  368. }
  369. }
  370. private void btnDelete_Click(object sender, EventArgs e)
  371. {
  372. if (syncInstrument == null) return;
  373. if (syncInstrument.lstFixedValue == null) return;
  374. if (dgvCnas == null) return;
  375. if (dgvCnas.Rows.Count <= 0) return;
  376. foreach (DataGridViewRow dr in dgvCnas.Rows)
  377. {
  378. dr.Cells["condition"].Value = "";
  379. dr.Cells["Value"].Value = "";
  380. }
  381. syncInstrument.lstFixedValue.Clear();
  382. }
  383. private void dgvCnas_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  384. {
  385. if (e.RowIndex <= 0) return;
  386. if (e.ColumnIndex == 0) //此时是“条件”列
  387. {
  388. if (dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value == null) return;
  389. if (dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value == null) return;
  390. if (dgvCnas.Rows[e.RowIndex].Cells["DataType"].Value == null) return;
  391. if (dgvCnas.Rows[e.RowIndex].Cells["Condition"].Value == null) return;
  392. //当前列名
  393. string strCurrentTable = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString();
  394. string strCurrentColumn = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString();
  395. string strCurrentType = dgvCnas.Rows[e.RowIndex].Cells["DataType"].Value.ToString();
  396. string strCurrentCondition = dgvCnas.Rows[e.RowIndex].Cells["Condition"].Value.ToString();
  397. string strCurrentValue = dgvCnas.Rows[e.RowIndex].Cells["Value"].Value == null ? "" : dgvCnas.Rows[e.RowIndex].Cells["Value"].Value.ToString();
  398. //将数据插入到数据源中
  399. var query = syncInstrument.lstFixedValue.Where(s => s.TableName == strCurrentTable && s.ColumnName == strCurrentColumn).ToList<CnasConditionMapValue>();
  400. if (query.Count >= 1)
  401. {
  402. GiveConditionByShow(query[0], strCurrentCondition);
  403. }
  404. else
  405. {
  406. CnasConditionMapValue cnasFixed = new CnasConditionMapValue();
  407. cnasFixed.TableName = strCurrentTable;
  408. cnasFixed.ColumnName = strCurrentColumn;
  409. cnasFixed.Value = strCurrentValue;
  410. GiveConditionByShow(cnasFixed, strCurrentCondition);
  411. syncInstrument.lstFixedValue.Add(cnasFixed);
  412. }
  413. }
  414. }
  415. private void dgvCnas_CellClick(object sender, DataGridViewCellEventArgs e)
  416. {
  417. cbxConditionValue.Visible = false;
  418. }
  419. private void ShowConditionValue()
  420. {
  421. Rectangle TmpRect = dgvCnas.GetCellDisplayRectangle(dgvCnas.CurrentCell.ColumnIndex, dgvCnas.CurrentCell.RowIndex, true);
  422. cbxConditionValue.Size = TmpRect.Size;
  423. cbxConditionValue.Top = TmpRect.Top;
  424. cbxConditionValue.Left = TmpRect.Left;
  425. cbxConditionValue.DropDownStyle = ComboBoxStyle.DropDownList;
  426. cbxConditionValue.FormattingEnabled = true;
  427. cbxConditionValue.Visible = true;
  428. if (!this.dgvCnas.Controls.Contains(cbxConditionValue))
  429. this.dgvCnas.Controls.Add(cbxConditionValue);
  430. }
  431. }
  432. }