DbFind.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. //赋值控件是否被GROUP包含
  151. Boolean groupcontains = false;
  152. //先判断DataTable里面是否有这个字段,然后从打开的窗口里面去获取到这个Form,从Form中的指定Panel获取到指定字段的控件
  153. FormCollection fmCollection = Application.OpenForms;
  154. SuccessReturnData = true;
  155. for (int i = 0; i < dt.Columns.Count; i++)
  156. {
  157. ControlCollection controls = (ControlCollection)fmCollection[FormName].Controls;
  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. 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))
  167. ct2.Text = DbFindGridView.Rows[e.RowIndex].Cells[dt.Columns[i].ColumnName].Value.ToString();
  168. }
  169. groupcontains = true;
  170. }
  171. break;
  172. }
  173. }
  174. if (!groupcontains)
  175. {
  176. for (int i = 0; i < dt.Columns.Count; i++)
  177. {
  178. for (int j = 0; j < SetValueField.Length; j++)
  179. {
  180. Control ctl = fmCollection[FormName].Controls[SetValueField[j]];
  181. 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))
  182. fmCollection[FormName].Controls[SetValueField[j]].Text = DbFindGridView.Rows[e.RowIndex].Cells[dt.Columns[i].ColumnName].Value.ToString();
  183. }
  184. }
  185. }
  186. fmCollection[FormName].Controls[MainField].Focus();
  187. }
  188. catch (Exception exa)
  189. {
  190. SuccessReturnData = false;
  191. }
  192. Dispose();
  193. Close();
  194. }
  195. //按下Esc键的时候关闭当前的界面,用于DbFind
  196. private void DbFindGridView_KeyPress(object sender, KeyPressEventArgs e)
  197. {
  198. if (e.KeyChar == (char)Keys.Escape)
  199. {
  200. Dispose();
  201. Close();
  202. }
  203. }
  204. //列宽发生变化的时候TextBox的宽度也发生变化
  205. private void DbFindGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
  206. {
  207. ctl[DbFindGridView.Columns.IndexOf(e.Column)].Width = e.Column.Width;
  208. }
  209. private void DbFindGridView_Scroll(object sender, ScrollEventArgs e)
  210. {
  211. //结合已拖动的长度和列宽计算此列当前展示的长度
  212. ScrollNewValue = e.NewValue;
  213. if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
  214. {
  215. int ColumnWidth = 0;
  216. for (int i = 0; i < dt.Columns.Count; i++)
  217. {
  218. ColumnWidth += DbFindGridView.Columns[i].Width;
  219. //如果有列被完全遮蔽,设置对应的输入框宽度为0
  220. if (ColumnWidth < ScrollNewValue)
  221. Controls[dt.Columns[i].Caption].Width = 0;
  222. else if (ColumnWidth > ScrollNewValue)
  223. {
  224. Controls[dt.Columns[i].Caption].Width = ColumnWidth - ScrollNewValue;
  225. break;
  226. }
  227. }
  228. }
  229. }
  230. }
  231. }