DbFind.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Data;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using UAS_LabelMachine.CustomControl;
  6. using UAS_LabelMachine.Entity;
  7. using UAS_LabelMachine.PublicMethod;
  8. namespace UAS_LabelMachine
  9. {
  10. public partial class DbFind : Form
  11. {
  12. bool IsAbleDbFind;
  13. string BindTable;
  14. DataHelper dh = SystemInf.dh;
  15. DataTable dt = new DataTable();
  16. AutoSizeFormClass asc = new AutoSizeFormClass();
  17. string TextBoxValue = "";
  18. Control[] ctl;
  19. //DBFind查询的字段
  20. string Field;
  21. //需要赋值的字段
  22. string[] SetValueField;
  23. //需要查询的全部字段
  24. string SelectField;
  25. //发起DbFind请求的窗口
  26. string FormName;
  27. //是否配置了DBfind
  28. string Caller;
  29. string Condition;
  30. public bool IsAbleDbFind1
  31. {
  32. get
  33. {
  34. return IsAbleDbFind;
  35. }
  36. set
  37. {
  38. IsAbleDbFind = value;
  39. }
  40. }
  41. public string TextBoxValue1
  42. {
  43. get
  44. {
  45. return TextBoxValue;
  46. }
  47. set
  48. {
  49. TextBoxValue = value;
  50. }
  51. }
  52. /// <summary>
  53. ///
  54. /// </summary>
  55. /// <param name="field"></param>
  56. /// <param name="setValueField"></param>
  57. /// <param name="caller"></param>
  58. /// <param name="formname"></param>
  59. public DbFind(string field, string tablename, string selectfield, string[] setValueField, string caller, string formname, string condition)
  60. {
  61. InitializeComponent();
  62. StartPosition = FormStartPosition.CenterParent;
  63. Field = field;
  64. FormName = formname;
  65. SetValueField = setValueField;
  66. Caller = caller;
  67. Condition = condition;
  68. BindTable = tablename;
  69. SelectField = selectfield.Replace("#", "as");
  70. //返回一个带有结构的空的DataTable
  71. dt = (DataTable)dh.ExecuteSql("select " + SelectField + " from " + tablename + " where rownum<20", "select");
  72. //设置DataTable的描述和列名,为了字段赋值
  73. selectfield = selectfield.Replace(",", "#");
  74. string[] NameAndCapation = selectfield.Split('#');
  75. int index = 0;
  76. for (int i = 0; i < dt.Columns.Count; i++)
  77. {
  78. dt.Columns[i].Caption = NameAndCapation[index].Trim();
  79. index = index + 1;
  80. dt.Columns[i].ColumnName = NameAndCapation[index].Trim();
  81. index = index + 1;
  82. }
  83. if (dt != null)
  84. {
  85. //先绑定空的结构
  86. DbFindGridView.DataSource = dt;
  87. for (int i = 0; i < DbFindGridView.Columns.Count; i++)
  88. {
  89. DbFindGridView.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  90. }
  91. //获取查询的字段的拼接语句
  92. pagination1.BindDataToNavigator(DbFindGridView, tablename, SelectField, "ID", caller, Condition == null ? "" : Condition);
  93. IsAbleDbFind = true;
  94. }
  95. }
  96. protected override void WndProc(ref Message m)
  97. {
  98. //拦截双击标题栏、移动窗体的系统消息
  99. if (m.Msg != 0xA3)
  100. {
  101. base.WndProc(ref m);
  102. }
  103. }
  104. private void DbFind_Load(object sender, EventArgs e)
  105. {
  106. asc.controllInitializeSize(this);
  107. //用来存放过滤的TextBox的控件
  108. ctl = new Control[dt.Columns.Count];
  109. //Dock是添加的越后面展示时越前面,所以需要从大到小排放
  110. for (int i = dt.Columns.Count - 1; i >= 0; i--)
  111. {
  112. EnterTextBox etb = new EnterTextBox();
  113. etb.Dock = DockStyle.Left;
  114. etb.Location = new Point(DbFindGridView.RowHeadersWidth, DbFindGridView.Columns[i].Width);
  115. etb.Name = dt.Columns[i].Caption;
  116. etb.Tag = dt.Columns[i].Caption;
  117. etb.Size = new Size(DbFindGridView.Columns[i].Width, 22);
  118. etb.KeyDown += FilterData;
  119. this.Controls.Add(etb);
  120. //记录这个生成的控件,后续用于拼接条件
  121. ctl[i] = etb;
  122. }
  123. //最后用一个不可编辑的占住头部长度
  124. EnterTextBox Head = new EnterTextBox();
  125. Head.Enabled = false;
  126. Head.Dock = DockStyle.Left;
  127. Head.Location = new Point(0, DbFindGridView.RowHeadersWidth);
  128. Head.Size = new Size(DbFindGridView.RowHeadersWidth, 22);
  129. this.Controls.Add(Head);
  130. }
  131. /// <summary>
  132. /// 过滤条件
  133. /// </summary>
  134. /// <param name="sender"></param>
  135. /// <param name="e"></param>
  136. private void FilterData(object sender, KeyEventArgs e)
  137. {
  138. if (e.KeyCode == Keys.Enter)
  139. pagination1.BindDataToNavigator(DbFindGridView, BindTable, SelectField, "ID", Caller, BaseUtil.GetScreenSqlCondition(ctl).Replace("where", "") + ((Condition == "" || Condition == null) ? "" : " and " + Condition));
  140. }
  141. private void DbFindGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  142. {
  143. try
  144. {
  145. //先判断DataTable里面是否有这个字段,然后从打开的窗口里面去获取到这个Form,从Form中的指定Panel获取到指定字段的控件
  146. for (int i = 0; i < dt.Columns.Count; i++)
  147. {
  148. for (int j = 0; j < SetValueField.Length; j++)
  149. {
  150. if (SetValueField[j] == dt.Columns[i].Caption || SetValueField[j] == dt.Columns[i].ColumnName || dt.Columns[i].Caption.Contains(SetValueField[j]))
  151. {
  152. FormCollection fmCollection = Application.OpenForms;
  153. fmCollection[FormName].Controls[SetValueField[j]].Text = DbFindGridView.Rows[e.RowIndex].Cells[dt.Columns[i].ColumnName].Value.ToString();
  154. }
  155. }
  156. }
  157. Dispose();
  158. Close();
  159. }
  160. catch (Exception)
  161. {
  162. Dispose();
  163. Close();
  164. }
  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. private void DbFind_SizeChanged(object sender, EventArgs e)
  176. {
  177. asc.controlAutoSize(this);
  178. }
  179. //列宽发生变化的时候TextBox的宽度也发生变化
  180. private void DbFindGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
  181. {
  182. if (ctl != null)
  183. ctl[DbFindGridView.Columns.IndexOf(e.Column)].Width = e.Column.Width;
  184. }
  185. }
  186. }