DbFind.cs 10 KB

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