DbFind.cs 11 KB

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