CNAS取数仪器端升级
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

541 行
23KB

  1. using CnasSynchronusClient;
  2. using CnasSynchrousModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. namespace CNAS_DBSync
  12. {
  13. public delegate void SourceDataFilterHandler(SourceDataFilter sourceDataFilter); //声明一个委托,用于向父窗口传输值
  14. public partial class frmSourceFilter : Form
  15. {
  16. public SourceDataFilterHandler sourceDataFilterHandler; //在子窗口中定义一个SourceDataFilterHandler类型委托
  17. public SyncInstrumentItemInfo syncInstrumentItem = new SyncInstrumentItemInfo();
  18. public DataTable dtCurrentTableStruct = new DataTable(); //当前表的列和数据类型
  19. private ComboBox cbxTableColumns; //定义一个ComboBox,用于选择列
  20. private ComboBox cbxAlgorithm; //定义一个ComboBox, 用于根据不同的列绑定不同的条件
  21. private ComboBox cbxConditionValue; //定义一个ComboBox,用于显示输入条件值的方式
  22. private TextBox txtConditionValue; //定义一个TextBox,用于输入自定义的条件值
  23. private List<string> lstIntOrDouble = new List<string>() { "等于", "不等于", "大于", "小于", "不大于", "不小于" };
  24. private List<string> lstText = new List<string>() { "等于", "不等于", "包含", "不包含" };
  25. private List<string> lstConditionValueType = new List<string>() { "{自定义...}","RegEx:{全数字}", "RegEx:{英文+数字}","RegEx:{中文+数字}" };
  26. public List<FilterCondition> lstFilterConditions { get; set; } //临时过滤条件
  27. public frmSourceFilter(SyncInstrumentItemInfo syncInstrumentItem)
  28. {
  29. InitializeComponent();
  30. this.syncInstrumentItem = syncInstrumentItem;
  31. if (syncInstrumentItem.SourceFilter == null || syncInstrumentItem.SourceFilter.lstFilterConditions == null)
  32. this.lstFilterConditions = new List<FilterCondition>();
  33. else
  34. this.lstFilterConditions =GlobalCommonOperation.Clone(syncInstrumentItem.SourceFilter.lstFilterConditions);
  35. dgvConditions.AutoGenerateColumns = false;
  36. }
  37. private void frmSourceFilter_Load(object sender, EventArgs e)
  38. {
  39. if (syncInstrumentItem == null) return;
  40. if (syncInstrumentItem.LstSyncPramas == null) return;
  41. if (syncInstrumentItem.LstSyncPramas.Count <= 0) return;
  42. //加载表名数据
  43. this.txtCurrentTableName.Text = syncInstrumentItem.LstSyncPramas[0].SourceTable;
  44. //加载表结构(需要重新加载,因为父窗口可能没有数据或数据已经过时)
  45. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(syncInstrumentItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  46. Dictionary<string,DataTable> dictInstruTables = instrumentData.GetInstrumentData();
  47. if (dictInstruTables.ContainsKey(this.txtCurrentTableName.Text))
  48. dtCurrentTableStruct = dictInstruTables[this.txtCurrentTableName.Text];
  49. //根据表结构手动构建ComboBox,用于用户选择增加列
  50. InitalColumnComboBox(dtCurrentTableStruct);
  51. InitalAlgorithmComboBox();
  52. InitalConditionValueComboBox();
  53. IntitalConditionValueTextBox();
  54. //加载过滤条件类型(与/或)
  55. if (syncInstrumentItem.SourceFilter == null)
  56. syncInstrumentItem.SourceFilter=new SourceDataFilter();
  57. if (syncInstrumentItem.SourceFilter.FilterConditionLinkType == null)
  58. syncInstrumentItem.SourceFilter.FilterConditionLinkType = "";
  59. this.cbxConditionLink.Text = ConvertLinkTypeToShow(syncInstrumentItem.SourceFilter.FilterConditionLinkType);
  60. //加载过滤条件
  61. if (lstFilterConditions == null)
  62. lstFilterConditions=new List<FilterCondition>();
  63. dgvConditions.DataSource = new BindingList<FilterCondition>();
  64. dgvConditions.DataSource = new BindingList<FilterCondition>(lstFilterConditions);
  65. }
  66. private void InitalColumnComboBox(DataTable dtCurrentTableStruct)
  67. {
  68. cbxTableColumns = new ComboBox();
  69. cbxTableColumns.SelectedIndexChanged += new EventHandler(cbxTableColumns_SelectedIndexChanged);
  70. cbxTableColumns.SelectionChangeCommitted += new EventHandler(cbxTableColumns_SelectionChangedCommitted);
  71. DataTable dtShow = new DataTable();
  72. dtShow.Columns.Add("InstruFieldName");
  73. dtShow.Columns.Add("InstruDataType");
  74. foreach (DataColumn dc in dtCurrentTableStruct.Columns)
  75. {
  76. dtShow.Rows.Add(new object[] { dc.ColumnName, dc.DataType });
  77. }
  78. cbxTableColumns.DataSource = dtShow;
  79. cbxTableColumns.DisplayMember = "InstruFieldName";
  80. cbxTableColumns.ValueMember = "InstruDataType";
  81. cbxTableColumns.Visible = false;
  82. }
  83. private void InitalAlgorithmComboBox()
  84. {
  85. cbxAlgorithm = new ComboBox();
  86. cbxAlgorithm.SelectedIndexChanged += new EventHandler(cbxAlgorithm_SelectedIndexChanged);
  87. cbxAlgorithm.SelectionChangeCommitted += new EventHandler(cbxAlgorithm_SelectionChangedCommitted);
  88. cbxAlgorithm.Visible = false;
  89. }
  90. private void InitalConditionValueComboBox()
  91. {
  92. cbxConditionValue = new ComboBox();
  93. cbxConditionValue.SelectedIndexChanged += new EventHandler(cbxConditionValue_SelectedIndexChanged);
  94. cbxConditionValue.SelectionChangeCommitted += new EventHandler(cbxConditionValue_SelectionChangedCommitted);
  95. cbxConditionValue.Visible = false;
  96. cbxConditionValue.DataSource = lstConditionValueType;
  97. }
  98. private void IntitalConditionValueTextBox()
  99. {
  100. txtConditionValue = new TextBox();
  101. txtConditionValue.Leave += new EventHandler(txtConditionValue_Leave);
  102. }
  103. private void cbxConditionValue_SelectionChangedCommitted(object sender, EventArgs e)
  104. {
  105. cbxConditionValue.Visible = false;
  106. }
  107. private void cbxConditionValue_SelectedIndexChanged(object sender, EventArgs e)
  108. {
  109. //当前选中单元格的值将随之发生更改
  110. if (dgvConditions.CurrentCell == null) return;
  111. string strConditionValue = "";
  112. switch (cbxConditionValue.Text)
  113. {
  114. case "RegEx:{全数字}":
  115. strConditionValue = @"RegEx:{^[1-9]\d*$}";
  116. dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value = strConditionValue;
  117. dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["Algorithm"].Value = "等于";
  118. break;
  119. case "RegEx:{英文+数字}":
  120. strConditionValue = @"RegEx:{[a-zA-Z][0-9]}";
  121. dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value = strConditionValue;
  122. dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["Algorithm"].Value = "等于";
  123. break;
  124. case "RegEx:{中文+数字}":
  125. strConditionValue = @"RegEx:{[\u4e00-\u9fa5][0-9]}";
  126. dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value = strConditionValue;
  127. dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["Algorithm"].Value = "等于";
  128. break;
  129. case "{自定义...}":
  130. default:
  131. //显示textbox控件,输入后,离开时提交数据。否则不提交任何数据
  132. ShowTextBox(dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value==null?"": dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value.ToString());
  133. //dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value = strConditionValue;
  134. break;
  135. }
  136. }
  137. private void cbxAlgorithm_SelectionChangedCommitted(object sender, EventArgs e)
  138. {
  139. cbxAlgorithm.Visible = false;
  140. }
  141. private void cbxAlgorithm_SelectedIndexChanged(object sender, EventArgs e)
  142. {
  143. //当前选中单元格的值将随之发生更改
  144. if (dgvConditions.CurrentCell == null) return;
  145. dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["Algorithm"].Value = cbxAlgorithm.Text;
  146. }
  147. private void cbxTableColumns_SelectionChangedCommitted(object sender, EventArgs e)
  148. {
  149. this.cbxTableColumns.Visible = false;
  150. }
  151. private void cbxTableColumns_SelectedIndexChanged(object sender, EventArgs e)
  152. {
  153. //当前选中单元格的值将随之发生更改
  154. if (dgvConditions.CurrentCell == null) return;
  155. dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ColumnName"].Value = cbxTableColumns.Text;
  156. dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ColumnDataType"].Value = cbxTableColumns.SelectedValue;
  157. }
  158. private string ConvertLinkTypeToShow(string filterConditionLinkType)
  159. {
  160. string strReturnValue = "";
  161. switch (filterConditionLinkType.ToLower())
  162. {
  163. case "or":
  164. strReturnValue = "或";
  165. break;
  166. case "and":
  167. default:
  168. strReturnValue = "与";
  169. break;
  170. }
  171. return strReturnValue;
  172. }
  173. /// <summary>
  174. /// 新增一个条件
  175. /// </summary>
  176. /// <param name="sender"></param>
  177. /// <param name="e"></param>
  178. private void btnAdd_Click(object sender, EventArgs e)
  179. {
  180. //最多支持5个条件,否则无限制的条件将极大影响性能
  181. if (lstFilterConditions.Count == 5)
  182. {
  183. MessageBox.Show("最多增加5项条件。");
  184. return;
  185. }
  186. //添加一条空数据
  187. FilterCondition condition = new FilterCondition();
  188. condition.TableName = this.txtCurrentTableName.Text;
  189. lstFilterConditions.Add(condition);
  190. //重新绑定
  191. dgvConditions.DataSource = new BindingList<FilterCondition>();
  192. dgvConditions.DataSource = new BindingList<FilterCondition>(lstFilterConditions);
  193. //选择最后一条
  194. dgvConditions.CurrentCell = dgvConditions.Rows[dgvConditions.Rows.Count - 1].Cells[1];
  195. }
  196. private void btnDelete_Click(object sender, EventArgs e)
  197. {
  198. if (dgvConditions.CurrentCell == null) return;
  199. if (dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ColumnName"].Value == null) return;
  200. if (dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value == null) return;
  201. if (dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["Algorithm"].Value == null) return;
  202. string strColumnName = dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ColumnName"].Value.ToString();
  203. string strConditionValue = dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value.ToString();
  204. string strAlgorithm = dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["Algorithm"].Value.ToString();
  205. var query = lstFilterConditions.Where(s => s.ColumnName == strColumnName && s.Algorithm == strAlgorithm && s.ConditionValue == strConditionValue);
  206. if (query.Count() > 0)
  207. {
  208. foreach (var item in query.ToList<FilterCondition>())
  209. {
  210. lstFilterConditions.Remove(item);
  211. }
  212. dgvConditions.DataSource = new BindingList<FilterCondition>(lstFilterConditions);
  213. }
  214. }
  215. private void dgvConditions_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  216. {
  217. cbxTableColumns.Visible = false;
  218. cbxAlgorithm.Visible = false;
  219. }
  220. private void dgvConditions_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  221. {
  222. cbxTableColumns.Visible = false;
  223. cbxAlgorithm.Visible = false;
  224. }
  225. private void dgvConditions_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  226. {
  227. if (dgvConditions.CurrentCell == null) return;
  228. if (dgvConditions.CurrentCell.ColumnIndex == 0) //此时表示准备编辑列名
  229. {
  230. ShowConditionColumnSelect();
  231. }
  232. if (dgvConditions.CurrentCell.ColumnIndex == 1) //此时表示准备编辑条件
  233. {
  234. ShowConditionAlgorithm();
  235. }
  236. if (dgvConditions.CurrentCell.ColumnIndex == 2) //此时表示准备编辑条件值
  237. {
  238. ShowConditionValue();
  239. }
  240. }
  241. /// <summary>
  242. /// 显示条件列
  243. /// </summary>
  244. private void ShowConditionColumnSelect()
  245. {
  246. Rectangle TmpRect = dgvConditions.GetCellDisplayRectangle(dgvConditions.CurrentCell.ColumnIndex, dgvConditions.CurrentCell.RowIndex, true);
  247. cbxTableColumns.Size = TmpRect.Size;
  248. cbxTableColumns.Top = TmpRect.Top;
  249. cbxTableColumns.Left = TmpRect.Left;
  250. cbxTableColumns.DropDownStyle = ComboBoxStyle.DropDownList;
  251. cbxTableColumns.FormattingEnabled = true;
  252. cbxTableColumns.Visible = true;
  253. if (!this.dgvConditions.Controls.Contains(cbxTableColumns))
  254. this.dgvConditions.Controls.Add(cbxTableColumns);
  255. }
  256. /// <summary>
  257. /// 显示选择运算条件
  258. /// </summary>
  259. private void ShowConditionAlgorithm()
  260. {
  261. //1.加载数据
  262. string strColumnName = "";
  263. if (dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ColumnName"].Value != null)
  264. strColumnName = dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ColumnName"].Value.ToString();
  265. if (strColumnName != "")
  266. {
  267. string strColumnType = dtCurrentTableStruct.Columns[strColumnName].DataType.ToString();
  268. switch (strColumnType)
  269. {
  270. case "System.DateTime":
  271. case "System.Double":
  272. case "System.Int64":
  273. case "System.Int32":
  274. case "System.Decimal":
  275. cbxAlgorithm.DataSource = lstIntOrDouble;
  276. break;
  277. case "System.Byte[]":
  278. case "System.String":
  279. default:
  280. cbxAlgorithm.DataSource = lstText;
  281. break;
  282. }
  283. }
  284. //2.在合适的位置显示控件
  285. Rectangle TmpRect = dgvConditions.GetCellDisplayRectangle(dgvConditions.CurrentCell.ColumnIndex, dgvConditions.CurrentCell.RowIndex, true);
  286. cbxAlgorithm.Size = TmpRect.Size;
  287. cbxAlgorithm.Top = TmpRect.Top;
  288. cbxAlgorithm.Left = TmpRect.Left;
  289. cbxAlgorithm.DropDownStyle = ComboBoxStyle.DropDownList;
  290. cbxAlgorithm.FormattingEnabled = true;
  291. cbxAlgorithm.Visible = true;
  292. if (!this.dgvConditions.Controls.Contains(cbxAlgorithm))
  293. this.dgvConditions.Controls.Add(cbxAlgorithm);
  294. }
  295. private void ShowConditionValue()
  296. {
  297. Rectangle TmpRect = dgvConditions.GetCellDisplayRectangle(dgvConditions.CurrentCell.ColumnIndex, dgvConditions.CurrentCell.RowIndex, true);
  298. cbxConditionValue.Size = TmpRect.Size;
  299. cbxConditionValue.Top = TmpRect.Top;
  300. cbxConditionValue.Left = TmpRect.Left;
  301. cbxConditionValue.DropDownStyle = ComboBoxStyle.DropDownList;
  302. cbxConditionValue.FormattingEnabled = true;
  303. cbxConditionValue.Visible = true;
  304. if (!this.dgvConditions.Controls.Contains(cbxConditionValue))
  305. this.dgvConditions.Controls.Add(cbxConditionValue);
  306. }
  307. private void ShowTextBox(string strConditionValue)
  308. {
  309. Rectangle TmpRect = dgvConditions.GetCellDisplayRectangle(dgvConditions.CurrentCell.ColumnIndex, dgvConditions.CurrentCell.RowIndex, true);
  310. txtConditionValue.Size = TmpRect.Size;
  311. txtConditionValue.Top = TmpRect.Top;
  312. txtConditionValue.Left = TmpRect.Left;
  313. txtConditionValue.Visible = true;
  314. txtConditionValue.Text = strConditionValue;
  315. if (!this.dgvConditions.Controls.Contains(txtConditionValue))
  316. this.dgvConditions.Controls.Add(txtConditionValue);
  317. }
  318. private void txtConditionValue_Leave(object sender, EventArgs e)
  319. {
  320. txtConditionValue.Visible = false;
  321. dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value = txtConditionValue.Text.Trim();
  322. }
  323. private void btnOK_Click(object sender, EventArgs e)
  324. {
  325. //初始化一个过滤器
  326. SourceDataFilter dataFilter = new SourceDataFilter {
  327. FilterConditionLinkType = this.cbxConditionLink.Text=="与"?"and":"or",
  328. lstFilterConditions=new List<FilterCondition>()
  329. };
  330. //添加过滤条件
  331. foreach (DataGridViewRow dgr in dgvConditions.Rows)
  332. {
  333. if (dgr.Cells["ColumnName"].Value == null) continue;
  334. if (dgr.Cells["Algorithm"].Value == null) continue;
  335. if (dgr.Cells["ColumnDataType"].Value == null) continue;
  336. if (dgr.Cells["ConditionValue"].Value == null) //此时需要判断列的数据类型
  337. {
  338. ////如果当前选择的条件列是字符串类型,则认为此处是空值,否则,必须手动输入值才能正常存储
  339. //if (dgr.Cells["ColumnDataType"].Value.ToString().ToLower() != "system.string") continue;
  340. dgr.Cells["ConditionValue"].Value = "";
  341. }
  342. string strColumnName = dgr.Cells["ColumnName"].Value.ToString();
  343. string strConditionValue = dgr.Cells["ConditionValue"].Value.ToString();
  344. string strAlgorithm = dgr.Cells["Algorithm"].Value.ToString();
  345. string strColumnDataType = dgr.Cells["ColumnDataType"].Value.ToString();
  346. FilterCondition condition = new FilterCondition
  347. {
  348. TableName = this.txtCurrentTableName.Text,
  349. ColumnName = strColumnName,
  350. ColumnDataType=strColumnDataType,
  351. Algorithm = ConvertShowAlgorithmToValue(strAlgorithm),
  352. ConditionValue = strConditionValue
  353. };
  354. if (!dataFilter.lstFilterConditions.Contains(condition))
  355. dataFilter.lstFilterConditions.Add(condition);
  356. }
  357. //使用委托返回过滤器
  358. sourceDataFilterHandler(dataFilter);
  359. this.Close();
  360. }
  361. private string ConvertShowAlgorithmToValue(string strAlgorithm)
  362. {
  363. string strValue = "";
  364. switch (strAlgorithm)
  365. {
  366. case "等于":
  367. strValue = "=";
  368. break;
  369. case "不等于":
  370. strValue = "<>";
  371. break;
  372. case "包含":
  373. strValue = "like";
  374. break;
  375. case "不包含":
  376. strValue = "not like";
  377. break;
  378. case "大于":
  379. strValue = ">";
  380. break;
  381. case "小于":
  382. strValue = "<";
  383. break;
  384. case "不大于":
  385. strValue = "<=";
  386. break;
  387. case "不小于":
  388. strValue = ">=";
  389. break;
  390. default:
  391. strValue = strAlgorithm;
  392. break;
  393. }
  394. return strValue;
  395. }
  396. private string ConvertAlgorithmValueToShow(string Algorithm)
  397. {
  398. string strValue = "";
  399. switch (Algorithm)
  400. {
  401. case "=":
  402. strValue = "等于";
  403. break;
  404. case "<>":
  405. strValue = "不等于";
  406. break;
  407. case "like":
  408. strValue = "包含";
  409. break;
  410. case "not like":
  411. strValue = "不包含";
  412. break;
  413. case ">":
  414. strValue = "大于";
  415. break;
  416. case "<":
  417. strValue = "小于";
  418. break;
  419. case "<=":
  420. strValue = "不大于";
  421. break;
  422. case ">=":
  423. strValue = "不小于";
  424. break;
  425. }
  426. return strValue;
  427. }
  428. private void dgvConditions_CellClick(object sender, DataGridViewCellEventArgs e)
  429. {
  430. cbxTableColumns.Visible = false;
  431. cbxAlgorithm.Visible = false;
  432. cbxConditionValue.Visible = false;
  433. txtConditionValue.Visible = false;
  434. }
  435. private void dgvConditions_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
  436. {
  437. if (e.ColumnIndex == 1&&e.Value!=null)
  438. {
  439. string strShowValue = ConvertAlgorithmValueToShow(e.Value.ToString());
  440. e.Value = strShowValue != ""?strShowValue: e.Value;
  441. }
  442. }
  443. #region 判断DGV是否点击空白
  444. public int GetRowIndexAt(int mouseLocation_Y,DataGridView dgv)
  445. {
  446. if (dgv.FirstDisplayedScrollingRowIndex < 0)
  447. {
  448. return -1;
  449. }
  450. if (dgv.ColumnHeadersVisible == true && mouseLocation_Y <= dgv.ColumnHeadersHeight)
  451. {
  452. return -1;
  453. }
  454. int index = dgv.FirstDisplayedScrollingRowIndex;
  455. int displayedCount = dgv.DisplayedRowCount(true);
  456. for (int k = 1; k <= displayedCount;)
  457. {
  458. if (dgv.Rows[index].Visible == true)
  459. {
  460. Rectangle rect = dgv.GetRowDisplayRectangle(index, true); // 取该区域的显示部分区域
  461. if (rect.Top <= mouseLocation_Y && mouseLocation_Y < rect.Bottom)
  462. {
  463. return index;
  464. }
  465. k++;
  466. }
  467. index++;
  468. }
  469. return -1;
  470. }
  471. #endregion
  472. private void dgvConditions_MouseClick(object sender, MouseEventArgs e)
  473. {
  474. if (GetRowIndexAt(e.Y, dgvConditions) == -1)
  475. {
  476. dgvConditions.EndEdit();
  477. cbxTableColumns.Visible = false;
  478. cbxAlgorithm.Visible = false;
  479. cbxConditionValue.Visible = false;
  480. txtConditionValue.Visible = false;
  481. }
  482. }
  483. }
  484. }