123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- using System;
- using System.Data;
- using System.Drawing;
- using System.Windows.Forms;
- using UAS_MES_NEW.CustomControl.TextBoxWithIcon;
- using UAS_MES_NEW.DataOperate;
- using UAS_MES_NEW.Entity;
- using UAS_MES_NEW.PublicMethod;
- namespace UAS_MES_NEW
- {
- public partial class DbFind : CustomControl.BaseForm.BaseForm
- {
- //判断是否配置了DbFind
- bool IsAbleDbFind;
- //双击选择的工单号
- string TextBoxValue = "";
- DataHelper dh = SystemInf.dh;
- DataTable dt = new DataTable();
- Control[] ctl;
- //DBFind查询的字段
- string MainField;
- //需要赋值的字段
- string[] SetValueField;
- //需要查询的全部字段
- string SelectField;
- //发起DbFind请求的窗口
- string FormName;
- //是否配置了DBfind
- string Caller;
- //需要查询的表
- string BindTable;
- string Condition = "";
- Control MainControl;
- public bool SuccessReturnData = false;
- int ScrollNewValue = 0;
- public bool IsAbleDbFind1
- {
- get
- {
- return IsAbleDbFind;
- }
- set
- {
- IsAbleDbFind = value;
- }
- }
- public string TextBoxValue1
- {
- get
- {
- return TextBoxValue;
- }
- set
- {
- TextBoxValue = value;
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="field"></param>
- /// <param name="setValueField"></param>
- /// <param name="caller"></param>
- /// <param name="formname"></param>
- public DbFind(string field, string tablename, string selectfield, string[] setValueField, string caller, string formname, string condition)
- {
- InitializeComponent();
- DoubleBuffered = true;
- try
- {
- StartPosition = FormStartPosition.CenterParent;
- MainField = field;
- FormName = formname;
- SetValueField = setValueField;
- Caller = caller;
- Condition = condition + "";
- BindTable = tablename;
- SelectField = selectfield.Replace("#", " as ");
- //返回一个带有结构的空的DataTable
- dt = (DataTable)dh.ExecuteSql("select " + SelectField + " from " + tablename + " where ROWNUM<20", "select");
- //设置DataTable的描述和列名,为了字段赋值
- selectfield = selectfield.Replace(",", "#");
- string[] NameAndCapation = selectfield.Split('#');
- int index = 0;
- //设置列的描述和名称
- for (int i = 0; i < dt.Columns.Count; i++)
- {
- dt.Columns[i].Caption = NameAndCapation[index].Trim();
- index = index + 1;
- dt.Columns[i].ColumnName = NameAndCapation[index].Trim();
- index = index + 1;
- }
- if (dt != null)
- {
- //先绑定空的结构
- DbFindGridView.DataSource = dt;
- for (int i = 0; i < DbFindGridView.Columns.Count; i++)
- {
- DbFindGridView.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
- }
- //获取查询的字段的拼接语句
- pagination1.BindDataToNavigator(DbFindGridView, tablename, SelectField, "ID", caller, Condition == null ? "" : Condition);
- IsAbleDbFind = true;
- }
- }
- catch (Exception EB)
- {
- LogManager.DoLog(EB.Message);
- IsAbleDbFind = false;
- }
- }
- protected override void WndProc(ref Message m)
- {
- //拦截双击标题栏、移动窗体的系统消息
- if (m.Msg != 0xA3)
- base.WndProc(ref m);
- }
- private void DbFind_Load(object sender, EventArgs e)
- {
- //用来存放过滤的TextBox的控件
- ctl = new Control[dt.Columns.Count];
- //Dock是添加的越后面展示时越前面,所以需要从大到小排放
- for (int i = dt.Columns.Count - 1; i >= 0; i--)
- {
- EnterTextBox etb = new EnterTextBox();
- etb.Dock = DockStyle.Left;
- etb.Location = new Point(DbFindGridView.RowHeadersWidth, DbFindGridView.Columns[i].Width);
- etb.Name = dt.Columns[i].Caption;
- etb.Tag = dt.Columns[i].Caption;
- etb.Size = new Size(DbFindGridView.Columns[i].Width, 22);
- etb.KeyDown += FilterData;
- this.Controls.Add(etb);
- //记录这个生成的控件,后续用于拼接条件
- ctl[i] = etb;
- }
- //最后用一个不可编辑的占住头部长度
- EnterTextBox Head = new EnterTextBox();
- Head.Name = "PlaceHolder";
- Head.Enabled = false;
- Head.Dock = DockStyle.Left;
- Head.Location = new Point(0, DbFindGridView.RowHeadersWidth);
- Head.Size = new Size(DbFindGridView.RowHeadersWidth, 22);
- this.Controls.Add(Head);
- }
- /// <summary>
- /// 过滤条件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void FilterData(object sender, KeyEventArgs e)
- {
- //筛选数据需要将当前页重置到1
- //并且手动执行一次刷新
- if (e.KeyCode == Keys.Enter)
- {
- pagination1.Current_Page = 1;
- pagination1.GetPageData();
- //所有输入框的条件拼接
- string filterCondition = BaseUtil.GetScreenSqlCondition(ctl).Replace("where", "").Trim();
- //拼接条件为空初始条件不为空
- if (filterCondition == "" && Condition != "")
- filterCondition = Condition;
- //拼接和初始条件都不为空
- else if (filterCondition != "" && Condition.Trim() != "")
- filterCondition = filterCondition + " and " + Condition;
- pagination1.BindDataToNavigator(DbFindGridView, BindTable, SelectField, "ID", Caller, filterCondition);
- }
- }
- //给打开窗体的对应字段赋值
- private void DbFindGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
- {
- if (e.RowIndex >= 0)
- {
- FormCollection fmCollection = Application.OpenForms;
- ControlCollection controls = (ControlCollection)fmCollection[FormName].Controls;
- try
- {
- //先判断DataTable里面是否有这个字段,然后从打开的窗口里面去获取到这个Form,从Form中的指定Panel获取到指定字段的控件
- for (int i = 0; i < dt.Columns.Count; i++)
- {
- fillControl(e, i, fmCollection[FormName]);
- }
- //发起DBFind的控件
- SuccessReturnData = true;
- if (MainControl is MaCodeSearchTextBox)
- {
- MaCodeSearchTextBox ctl = (MainControl as MaCodeSearchTextBox);
- ctl.GetData(true);
- }
- if (MainControl is SearchTextBox)
- {
- SearchTextBox ctl = (MainControl as SearchTextBox);
- ctl.GetData();
- }
- MainControl.Focus();
- }
- catch (Exception ea)
- {
- LogManager.DoLog(ea.Message);
- SuccessReturnData = false;
- }
- dt = (DataTable)DbFindGridView.DataSource;
- for (int i = 0; i < dt.Columns.Count; i++)
- {
- if (MainControl.Name == dt.Columns[i].Caption)
- {
- TextBoxValue = dt.Rows[e.RowIndex][i].ToString();
- break;
- }
- }
- Dispose();
- Close();
- }
- }
- private void fillControl(DataGridViewCellEventArgs e, int i, Control ct)
- {
- for (int j = 0; j < SetValueField.Length; j++)
- {
- if (ct.Controls.Count > 0 && ct.Name.ToString() != SetValueField[j])
- {
- Control.ControlCollection controls = ct.Controls;
- for (int k = 0; k < ct.Controls.Count; k++)
- {
- fillControl(e, i, controls[k]);
- }
- }
- else
- {
- if (ct.Name == MainField)
- {
- MainControl = ct;
- }
- if ((SetValueField[j] == dt.Columns[i].Caption || SetValueField[j] == dt.Columns[i].ColumnName || SetValueField[j].Contains(dt.Columns[i].Caption)) && ct.Name.ToString().ToUpper() == SetValueField[j].ToUpper())
- try
- {
- if (ct.Tag.ToString() == "add" && ct.Text != "")
- {
- if (!ct.Text.Contains(DbFindGridView.Rows[e.RowIndex].Cells[dt.Columns[i].ColumnName].Value.ToString()))
- {
- ct.Text = ct.Text + "|" + DbFindGridView.Rows[e.RowIndex].Cells[dt.Columns[i].ColumnName].Value.ToString();
- }
- }
- else
- {
- ct.Text = DbFindGridView.Rows[e.RowIndex].Cells[dt.Columns[i].ColumnName].Value.ToString();
- }
- }
- catch {
- ct.Text = DbFindGridView.Rows[e.RowIndex].Cells[dt.Columns[i].ColumnName].Value.ToString();
- }
- }
- }
- }
- //按下Esc键的时候关闭当前的界面,用于DbFind
- private void DbFindGridView_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == (char)Keys.Escape)
- {
- Dispose();
- Close();
- }
- }
- //列宽发生变化的时候TextBox的宽度也发生变化
- private void DbFindGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
- {
- if (ctl != null)
- ctl[DbFindGridView.Columns.IndexOf(e.Column)].Width = e.Column.Width;
- }
- private void DbFindGridView_Scroll(object sender, ScrollEventArgs e)
- {
- //结合已拖动的长度和列宽计算此列当前展示的长度
- ScrollNewValue = e.NewValue;
- if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
- {
- int ColumnWidth = 0;
- for (int i = 0; i < dt.Columns.Count; i++)
- {
- ColumnWidth += DbFindGridView.Columns[i].Width;
- //如果有列被完全遮蔽,设置对应的输入框宽度为0
- if (ColumnWidth < ScrollNewValue)
- Controls[dt.Columns[i].Caption].Width = 0;
- else if (ColumnWidth > ScrollNewValue)
- {
- Controls[dt.Columns[i].Caption].Width = ColumnWidth - ScrollNewValue;
- break;
- }
- }
- }
- }
- private void DbFind_FormClosing(object sender, FormClosingEventArgs e)
- {
- dh.Dispose();
- }
- }
- }
|