DbFind.cs 8.5 KB

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