DbFind.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using System;
  2. using System.Data;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using UAS_MES.CustomControl.TextBoxWithIcon;
  6. using UAS_MES.DataOperate;
  7. using UAS_MES.PublicMethod;
  8. namespace UAS_MES
  9. {
  10. public partial class DbFind : Form
  11. {
  12. //判断是否配置了DbFind
  13. bool IsAbleDbFind;
  14. DataHelper dh = new DataHelper();
  15. DataTable dt = new DataTable();
  16. Control[] ctl;
  17. //DBFind查询的字段
  18. string MainField;
  19. //需要赋值的字段
  20. string[] SetValueField;
  21. //需要查询的全部字段
  22. string SelectField;
  23. //发起DbFind请求的窗口
  24. string FormName;
  25. //是否配置了DBfind
  26. string Caller;
  27. //需要查询的表
  28. string BindTable;
  29. string Condition = "";
  30. Control MainControl;
  31. public bool SuccessReturnData = false;
  32. int ScrollNewValue = 0;
  33. public bool IsAbleDbFind1
  34. {
  35. get
  36. {
  37. return IsAbleDbFind;
  38. }
  39. set
  40. {
  41. IsAbleDbFind = value;
  42. }
  43. }
  44. /// <summary>
  45. ///
  46. /// </summary>
  47. /// <param name="field"></param>
  48. /// <param name="setValueField"></param>
  49. /// <param name="caller"></param>
  50. /// <param name="formname"></param>
  51. public DbFind(string field, string tablename, string selectfield, string[] setValueField, string caller, string formname, string condition)
  52. {
  53. InitializeComponent();
  54. DoubleBuffered = true;
  55. try
  56. {
  57. StartPosition = FormStartPosition.CenterParent;
  58. MainField = field;
  59. FormName = formname;
  60. SetValueField = setValueField;
  61. Caller = caller;
  62. Condition = condition + "";
  63. BindTable = tablename;
  64. SelectField = selectfield.Replace("#", " as ");
  65. //返回一个带有结构的空的DataTable
  66. dt = (DataTable)dh.ExecuteSql("select " + SelectField + " from " + tablename + " where ROWNUM<20", "select");
  67. //设置DataTable的描述和列名,为了字段赋值
  68. selectfield = selectfield.Replace(",", "#");
  69. string[] NameAndCapation = selectfield.Split('#');
  70. int index = 0;
  71. //设置列的描述和名称
  72. for (int i = 0; i < dt.Columns.Count; i++)
  73. {
  74. dt.Columns[i].Caption = NameAndCapation[index].Trim();
  75. index = index + 1;
  76. dt.Columns[i].ColumnName = NameAndCapation[index].Trim();
  77. index = index + 1;
  78. }
  79. if (dt != null)
  80. {
  81. //先绑定空的结构
  82. DbFindGridView.DataSource = dt;
  83. //获取查询的字段的拼接语句
  84. pagination1.BindDataToNavigator(DbFindGridView, tablename, SelectField, "ID", caller, Condition == null ? "" : Condition);
  85. IsAbleDbFind = true;
  86. }
  87. }
  88. catch (Exception EB) {
  89. LogManager.DoLog(EB.Message);
  90. IsAbleDbFind = false; }
  91. }
  92. protected override void WndProc(ref Message m)
  93. {
  94. //拦截双击标题栏、移动窗体的系统消息
  95. if (m.Msg != 0xA3)
  96. base.WndProc(ref m);
  97. }
  98. private void DbFind_Load(object sender, EventArgs e)
  99. {
  100. //用来存放过滤的TextBox的控件
  101. ctl = new Control[dt.Columns.Count];
  102. //Dock是添加的越后面展示时越前面,所以需要从大到小排放
  103. for (int i = dt.Columns.Count - 1; i >= 0; i--)
  104. {
  105. EnterTextBox etb = new EnterTextBox();
  106. etb.Dock = DockStyle.Left;
  107. etb.Location = new Point(DbFindGridView.RowHeadersWidth, DbFindGridView.Columns[i].Width);
  108. etb.Name = dt.Columns[i].Caption;
  109. etb.Tag = dt.Columns[i].Caption;
  110. etb.Size = new Size(DbFindGridView.Columns[i].Width, 22);
  111. etb.KeyDown += FilterData;
  112. this.Controls.Add(etb);
  113. //记录这个生成的控件,后续用于拼接条件
  114. ctl[i] = etb;
  115. }
  116. //最后用一个不可编辑的占住头部长度
  117. EnterTextBox Head = new EnterTextBox();
  118. Head.Name = "PlaceHolder";
  119. Head.Enabled = false;
  120. Head.Dock = DockStyle.Left;
  121. Head.Location = new Point(0, DbFindGridView.RowHeadersWidth);
  122. Head.Size = new Size(DbFindGridView.RowHeadersWidth, 22);
  123. this.Controls.Add(Head);
  124. }
  125. /// <summary>
  126. /// 过滤条件
  127. /// </summary>
  128. /// <param name="sender"></param>
  129. /// <param name="e"></param>
  130. private void FilterData(object sender, KeyEventArgs e)
  131. {
  132. //筛选数据需要将当前页重置到1
  133. //并且手动执行一次刷新
  134. if (e.KeyCode == Keys.Enter)
  135. {
  136. pagination1.Current_Page = 1;
  137. pagination1.GetPageData();
  138. //所有输入框的条件拼接
  139. string filterCondition = BaseUtil.GetScreenSqlCondition(ctl).Replace("where", "").Trim();
  140. //拼接条件为空初始条件不为空
  141. if (filterCondition == "" && Condition != "")
  142. filterCondition = Condition;
  143. //拼接和初始条件都不为空
  144. else if (filterCondition != "" && Condition.Trim() != "")
  145. filterCondition = filterCondition + " and " + Condition;
  146. pagination1.BindDataToNavigator(DbFindGridView, BindTable, SelectField, "ID", Caller, filterCondition);
  147. }
  148. }
  149. //给打开窗体的对应字段赋值
  150. private void DbFindGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  151. {
  152. FormCollection fmCollection = Application.OpenForms;
  153. ControlCollection controls = (ControlCollection)fmCollection[FormName].Controls;
  154. try
  155. {
  156. //先判断DataTable里面是否有这个字段,然后从打开的窗口里面去获取到这个Form,从Form中的指定Panel获取到指定字段的控件
  157. for (int i = 0; i < dt.Columns.Count; i++)
  158. {
  159. fillControl(e, i, fmCollection[FormName]);
  160. }
  161. //发起DBFind的控件
  162. SuccessReturnData = true;
  163. if (MainControl is MaCodeSearchTextBox)
  164. {
  165. MaCodeSearchTextBox ctl = (MainControl as MaCodeSearchTextBox);
  166. ctl.GetData();
  167. }
  168. if (MainControl is SearchTextBox)
  169. {
  170. SearchTextBox ctl = (MainControl as SearchTextBox);
  171. ctl.GetData();
  172. }
  173. MainControl.Focus();
  174. }
  175. catch (Exception ea)
  176. {
  177. LogManager.DoLog(ea.Message);
  178. MainControl = fillFormname(FormName, (ControlCollection)fmCollection[FormName].Controls, e);
  179. if (MainControl is MaCodeSearchTextBox)
  180. {
  181. MaCodeSearchTextBox ctl = (MainControl as MaCodeSearchTextBox);
  182. ctl.GetData();
  183. }
  184. if (MainControl is SearchTextBox)
  185. {
  186. SearchTextBox ctl = (MainControl as SearchTextBox);
  187. ctl.GetData();
  188. }
  189. MainControl.Focus();
  190. SuccessReturnData = false;
  191. }
  192. Dispose();
  193. Close();
  194. }
  195. private Control fillFormname(string fromname, ControlCollection controls, DataGridViewCellEventArgs e)
  196. {
  197. Control ct = null;
  198. if (e.RowIndex >= 0)
  199. {
  200. switch (fromname)
  201. {
  202. case "Make_TestCollection":
  203. controls[MainField].Text = DbFindGridView.Rows[e.RowIndex].Cells["工单号"].Value.ToString();
  204. ct = controls[MainField];
  205. break;
  206. case "Make_Repair":
  207. if (MainField == "cr_code")
  208. {
  209. controls[MainField].Text = DbFindGridView.Rows[e.RowIndex].Cells["工艺编号"].Value.ToString();
  210. }
  211. else if (MainField == "cd_stepcode")
  212. {
  213. controls[MainField].Text = DbFindGridView.Rows[e.RowIndex].Cells["工序编号"].Value.ToString();
  214. }
  215. else
  216. {
  217. controls[MainField].Text = DbFindGridView.Rows[e.RowIndex].Cells["零件名称"].Value.ToString();
  218. }
  219. ct = controls[MainField];
  220. break;
  221. case "OQC_PlanMaintain":
  222. if (MainField == "al_code")
  223. {
  224. Control.ControlCollection ctL = controls["panel1"].Controls;
  225. ctL[MainField].Text = DbFindGridView.Rows[e.RowIndex].Cells["流水编号"].Value.ToString();
  226. ct = ctL[MainField];
  227. }
  228. else
  229. {
  230. controls[MainField].Text = DbFindGridView.Rows[e.RowIndex].Cells["方案编号"].Value.ToString();
  231. ct = controls[MainField];
  232. }
  233. break;
  234. case "OQC_Inspection":
  235. controls[MainField].Text = DbFindGridView.Rows[e.RowIndex].Cells["工单号"].Value.ToString();
  236. ct = controls[MainField];
  237. break;
  238. case "Make_FeedingCollection":
  239. controls[MainField].Text = DbFindGridView.Rows[e.RowIndex].Cells["工单号"].Value.ToString();
  240. ct = controls[MainField];
  241. break;
  242. }
  243. }
  244. return ct;
  245. }
  246. private void fillControl(DataGridViewCellEventArgs e, int i, Control ct)
  247. {
  248. for (int j = 0; j < SetValueField.Length; j++)
  249. {
  250. if (ct.Controls.Count > 0 && ct.Name.ToString() != SetValueField[j])
  251. {
  252. Control.ControlCollection controls = ct.Controls;
  253. for (int k = 0; k < ct.Controls.Count; k++)
  254. {
  255. fillControl(e, i, controls[k]);
  256. }
  257. }
  258. else
  259. {
  260. if ((SetValueField[j] == dt.Columns[i].Caption || SetValueField[j] == dt.Columns[i].ColumnName || SetValueField[j].Contains(dt.Columns[i].Caption) || (ct != null && ct.Tag != null && ct.Tag.ToString() == dt.Columns[i].Caption)) && ct.Name.ToString() == SetValueField[j])
  261. ct.Text = DbFindGridView.Rows[e.RowIndex].Cells[dt.Columns[i].ColumnName].Value.ToString();
  262. if (ct.Name == MainField)
  263. {
  264. MainControl = ct;
  265. }
  266. }
  267. }
  268. }
  269. //按下Esc键的时候关闭当前的界面,用于DbFind
  270. private void DbFindGridView_KeyPress(object sender, KeyPressEventArgs e)
  271. {
  272. if (e.KeyChar == (char)Keys.Escape)
  273. {
  274. Dispose();
  275. Close();
  276. }
  277. }
  278. //列宽发生变化的时候TextBox的宽度也发生变化
  279. private void DbFindGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
  280. {
  281. ctl[DbFindGridView.Columns.IndexOf(e.Column)].Width = e.Column.Width;
  282. }
  283. private void DbFindGridView_Scroll(object sender, ScrollEventArgs e)
  284. {
  285. //结合已拖动的长度和列宽计算此列当前展示的长度
  286. ScrollNewValue = e.NewValue;
  287. if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
  288. {
  289. int ColumnWidth = 0;
  290. for (int i = 0; i < dt.Columns.Count; i++)
  291. {
  292. ColumnWidth += DbFindGridView.Columns[i].Width;
  293. //如果有列被完全遮蔽,设置对应的输入框宽度为0
  294. if (ColumnWidth < ScrollNewValue)
  295. Controls[dt.Columns[i].Caption].Width = 0;
  296. else if (ColumnWidth > ScrollNewValue)
  297. {
  298. Controls[dt.Columns[i].Caption].Width = ColumnWidth - ScrollNewValue;
  299. break;
  300. }
  301. }
  302. }
  303. }
  304. }
  305. }