CNAS取数仪器端升级
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

frmCNASValue.cs 20KB

pirms 5 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. default:
  184. break;
  185. }
  186. }
  187. public string GiveShowByCondition(CnasConditionMapValue cnasFixed)
  188. {
  189. string strCurrentCondition = "";
  190. switch (cnasFixed.Condition)
  191. {
  192. case MapCondition.Equal:
  193. strCurrentCondition = "等于";
  194. break;
  195. case MapCondition.Sub:
  196. strCurrentCondition = "连接";
  197. break;
  198. case MapCondition.IFThen:
  199. strCurrentCondition = "若...则...";
  200. break;
  201. case MapCondition.Divided:
  202. strCurrentCondition = "除以";
  203. break;
  204. case MapCondition.Multiplied:
  205. strCurrentCondition = "乘以";
  206. break;
  207. case MapCondition.SubString:
  208. strCurrentCondition = "截取";
  209. break;
  210. case MapCondition.DecimalDigits:
  211. strCurrentCondition = "小数位数";
  212. break;
  213. }
  214. return strCurrentCondition;
  215. }
  216. private void dgvCnas_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  217. {
  218. Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
  219. e.RowBounds.Location.Y,
  220. dgvCnas.RowHeadersWidth - 4,
  221. e.RowBounds.Height);
  222. TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
  223. dgvCnas.RowHeadersDefaultCellStyle.Font,
  224. rectangle,
  225. dgvCnas.RowHeadersDefaultCellStyle.ForeColor,
  226. TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
  227. }
  228. private void dgvCnas_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  229. {
  230. if (e.ColumnIndex == 0) //如果此时是“条件”列
  231. {
  232. ShowConditionValue();
  233. }
  234. if (e.ColumnIndex == 1)//如果此时是“值”列
  235. {
  236. if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value == null) return;
  237. if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value.ToString() == "连接")
  238. {
  239. CnasConditionMapValue cnasFixed = new CnasConditionMapValue
  240. {
  241. TableName = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString(),
  242. ColumnName = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString(),
  243. Condition = MapCondition.Sub
  244. };
  245. if (dgvCnas.Rows[e.RowIndex].Cells["Value"].Value!=null)
  246. cnasFixed.Value= dgvCnas.Rows[e.RowIndex].Cells["Value"].Value.ToString();
  247. frmConditionParam frmCondition = new frmConditionParam(syncInstrument, cnasFixed);
  248. if (frmCondition.ShowDialog() == DialogResult.OK)
  249. {
  250. if (frmCondition.conditionvalue.Value != null&& frmCondition.conditionvalue.Value.ToString()!="")
  251. {
  252. dgvCnas.Rows[e.RowIndex].Cells["Value"].Value = frmCondition.conditionvalue.Value;
  253. //将数据插入到数据源中
  254. var query = syncInstrument.lstFixedValue.Where(s => s.TableName == cnasFixed.TableName && s.ColumnName == cnasFixed.ColumnName).ToList<CnasConditionMapValue>();
  255. if (query.Count >= 1)
  256. {
  257. query[0].Condition = cnasFixed.Condition;
  258. query[0].Value = frmCondition.conditionvalue.Value;
  259. }
  260. else
  261. {
  262. cnasFixed.Value = frmCondition.conditionvalue.Value;
  263. syncInstrument.lstFixedValue.Add(cnasFixed);
  264. }
  265. }
  266. }
  267. }
  268. if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value.ToString() == "若...则...")
  269. {
  270. CnasConditionMapValue cnasFixed = new CnasConditionMapValue
  271. {
  272. TableName = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString(),
  273. ColumnName = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString(),
  274. Condition = MapCondition.IFThen
  275. };
  276. if (dgvCnas.Rows[e.RowIndex].Cells["Value"].Value != null)
  277. cnasFixed.Value = dgvCnas.Rows[e.RowIndex].Cells["Value"].Value.ToString();
  278. frmIfThenParams frmIfThen = new frmIfThenParams(syncInstrument, cnasFixed);
  279. if (frmIfThen.ShowDialog() == DialogResult.OK)
  280. {
  281. if (frmIfThen.conditionvalue.Value != null && frmIfThen.conditionvalue.Value.ToString() != "")
  282. {
  283. dgvCnas.Rows[e.RowIndex].Cells["Value"].Value = frmIfThen.conditionvalue.Value;
  284. //将数据插入到数据源中
  285. var query = syncInstrument.lstFixedValue.Where(s => s.TableName == cnasFixed.TableName && s.ColumnName == cnasFixed.ColumnName).ToList<CnasConditionMapValue>();
  286. if (query.Count >= 1)
  287. {
  288. query[0].Condition = cnasFixed.Condition;
  289. query[0].Value = frmIfThen.conditionvalue.Value;
  290. }
  291. else
  292. {
  293. cnasFixed.Value = frmIfThen.conditionvalue.Value;
  294. syncInstrument.lstFixedValue.Add(cnasFixed);
  295. }
  296. }
  297. }
  298. }
  299. if (dgvCnas.Rows[e.RowIndex].Cells["condition"].Value.ToString() == "截取")
  300. {
  301. CnasConditionMapValue cnasFixed = new CnasConditionMapValue
  302. {
  303. TableName = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString(),
  304. ColumnName = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString(),
  305. Condition = MapCondition.SubString
  306. };
  307. if (dgvCnas.Rows[e.RowIndex].Cells["Value"].Value != null)
  308. cnasFixed.Value = dgvCnas.Rows[e.RowIndex].Cells["Value"].Value.ToString();
  309. frmSplitParam frmSplitParam = new frmSplitParam(cnasFixed);
  310. if (frmSplitParam.ShowDialog() == DialogResult.OK)
  311. {
  312. if (frmSplitParam.conditionvalue.Value != null && frmSplitParam.conditionvalue.Value.ToString() != "")
  313. {
  314. dgvCnas.Rows[e.RowIndex].Cells["Value"].Value = frmSplitParam.conditionvalue.Value;
  315. //将数据插入到数据源中
  316. var query = syncInstrument.lstFixedValue.Where(s => s.TableName == cnasFixed.TableName && s.ColumnName == cnasFixed.ColumnName).ToList<CnasConditionMapValue>();
  317. if (query.Count >= 1)
  318. {
  319. query[0].Condition = cnasFixed.Condition;
  320. query[0].Value = frmSplitParam.conditionvalue.Value;
  321. }
  322. else
  323. {
  324. cnasFixed.Value = frmSplitParam.conditionvalue.Value;
  325. syncInstrument.lstFixedValue.Add(cnasFixed);
  326. }
  327. }
  328. }
  329. }
  330. dgvCnas.EndEdit();
  331. }
  332. }
  333. private void btnDelete_Click(object sender, EventArgs e)
  334. {
  335. if (syncInstrument == null) return;
  336. if (syncInstrument.lstFixedValue == null) return;
  337. if (dgvCnas == null) return;
  338. if (dgvCnas.Rows.Count <= 0) return;
  339. foreach (DataGridViewRow dr in dgvCnas.Rows)
  340. {
  341. dr.Cells["condition"].Value = "";
  342. dr.Cells["Value"].Value = "";
  343. }
  344. syncInstrument.lstFixedValue.Clear();
  345. }
  346. private void dgvCnas_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  347. {
  348. if (e.RowIndex <= 0) return;
  349. if (e.ColumnIndex == 0) //此时是“条件”列
  350. {
  351. if (dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value == null) return;
  352. if (dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value == null) return;
  353. if (dgvCnas.Rows[e.RowIndex].Cells["DataType"].Value == null) return;
  354. if (dgvCnas.Rows[e.RowIndex].Cells["Condition"].Value == null) return;
  355. //当前列名
  356. string strCurrentTable = dgvCnas.Rows[e.RowIndex].Cells["TableName"].Value.ToString();
  357. string strCurrentColumn = dgvCnas.Rows[e.RowIndex].Cells["TableColumn"].Value.ToString();
  358. string strCurrentType = dgvCnas.Rows[e.RowIndex].Cells["DataType"].Value.ToString();
  359. string strCurrentCondition = dgvCnas.Rows[e.RowIndex].Cells["Condition"].Value.ToString();
  360. string strCurrentValue = dgvCnas.Rows[e.RowIndex].Cells["Value"].Value == null ? "" : dgvCnas.Rows[e.RowIndex].Cells["Value"].Value.ToString();
  361. //将数据插入到数据源中
  362. var query = syncInstrument.lstFixedValue.Where(s => s.TableName == strCurrentTable && s.ColumnName == strCurrentColumn).ToList<CnasConditionMapValue>();
  363. if (query.Count >= 1)
  364. {
  365. GiveConditionByShow(query[0], strCurrentCondition);
  366. }
  367. else
  368. {
  369. CnasConditionMapValue cnasFixed = new CnasConditionMapValue();
  370. cnasFixed.TableName = strCurrentTable;
  371. cnasFixed.ColumnName = strCurrentColumn;
  372. cnasFixed.Value = strCurrentValue;
  373. GiveConditionByShow(cnasFixed, strCurrentCondition);
  374. syncInstrument.lstFixedValue.Add(cnasFixed);
  375. }
  376. }
  377. }
  378. private void dgvCnas_CellClick(object sender, DataGridViewCellEventArgs e)
  379. {
  380. cbxConditionValue.Visible = false;
  381. }
  382. private void ShowConditionValue()
  383. {
  384. Rectangle TmpRect = dgvCnas.GetCellDisplayRectangle(dgvCnas.CurrentCell.ColumnIndex, dgvCnas.CurrentCell.RowIndex, true);
  385. cbxConditionValue.Size = TmpRect.Size;
  386. cbxConditionValue.Top = TmpRect.Top;
  387. cbxConditionValue.Left = TmpRect.Left;
  388. cbxConditionValue.DropDownStyle = ComboBoxStyle.DropDownList;
  389. cbxConditionValue.FormattingEnabled = true;
  390. cbxConditionValue.Visible = true;
  391. if (!this.dgvCnas.Controls.Contains(cbxConditionValue))
  392. this.dgvCnas.Controls.Add(cbxConditionValue);
  393. }
  394. }
  395. }