using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using UAS_MES.CustomControl.TextBoxWithIcon;
using UAS_MES.DataOperate;
using UAS_MES.PublicMethod;
namespace UAS_MES
{
public partial class DbFind : Form
{
//判断是否配置了DbFind
bool IsAbleDbFind;
DataHelper dh = new DataHelper();
DataTable dt = new DataTable();
Control[] ctl;
//DBFind查询的字段
string MainField;
//需要赋值的字段
string[] SetValueField;
//需要查询的全部字段
string SelectField;
//发起DbFind请求的窗口
string FormName;
//是否配置了DBfind
string Caller;
//需要查询的表
string BindTable;
string Condition = "";
public bool SuccessReturnData = false;
int ScrollNewValue = 0;
public bool IsAbleDbFind1
{
get
{
return IsAbleDbFind;
}
set
{
IsAbleDbFind = value;
}
}
///
///
///
///
///
///
///
public DbFind(string field, string tablename, string selectfield, string[] setValueField, string caller, string formname, string condition)
{
InitializeComponent();
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;
//获取查询的字段的拼接语句
pagination1.BindDataToNavigator(DbFindGridView, tablename, SelectField, "ID", caller, Condition == null ? "" : Condition);
IsAbleDbFind = true;
}
}
catch (Exception) { 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);
}
///
/// 过滤条件
///
///
///
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)
{
try
{
//赋值控件是否被GROUP包含
Boolean groupcontains = false;
//先判断DataTable里面是否有这个字段,然后从打开的窗口里面去获取到这个Form,从Form中的指定Panel获取到指定字段的控件
FormCollection fmCollection = Application.OpenForms;
SuccessReturnData = true;
ControlCollection controls = (ControlCollection)fmCollection[FormName].Controls;
for (int i = 0; i < dt.Columns.Count; i++)
{
for (int k = 0; k < controls.Count; k++)
{
Control ctl = controls[k];
if (ctl.Controls.Count > 0)
{
for (int j = 0; j < SetValueField.Length; j++)
{
Control ct2 = ctl.Controls[SetValueField[j]];
if (ct2 != null)
{
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))
ct2.Text = DbFindGridView.Rows[e.RowIndex].Cells[dt.Columns[i].ColumnName].Value.ToString();
groupcontains = true;
}
}
}
break;
}
}
if (!groupcontains)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
for (int j = 0; j < SetValueField.Length; j++)
{
Control ctl = fmCollection[FormName].Controls[SetValueField[j]];
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))
fmCollection[FormName].Controls[SetValueField[j]].Text = DbFindGridView.Rows[e.RowIndex].Cells[dt.Columns[i].ColumnName].Value.ToString();
}
}
}
fmCollection[FormName].Controls[MainField].Focus();
}
catch (Exception exa)
{
SuccessReturnData = false;
}
Dispose();
Close();
}
//按下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)
{
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;
}
}
}
}
}
}