DbFind.cs 8.7 KB

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