DbFind.cs 11 KB

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