DbFind.cs 11 KB

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