using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using UAS_LabelMachine.CustomControl;
using UAS_LabelMachine.Entity;
using UAS_LabelMachine.PublicMethod;
namespace UAS_LabelMachine
{
public partial class DbFind : Form
{
bool IsAbleDbFind;
string BindTable;
DataHelper dh = SystemInf.dh;
DataTable dt = new DataTable();
AutoSizeFormClass asc = new AutoSizeFormClass();
string TextBoxValue = "";
Control[] ctl;
//DBFind查询的字段
string Field;
//需要赋值的字段
string[] SetValueField;
//需要查询的全部字段
string SelectField;
//发起DbFind请求的窗口
string FormName;
//是否配置了DBfind
string Caller;
string Condition;
public bool IsAbleDbFind1
{
get
{
return IsAbleDbFind;
}
set
{
IsAbleDbFind = value;
}
}
public string TextBoxValue1
{
get
{
return TextBoxValue;
}
set
{
TextBoxValue = value;
}
}
///
///
///
///
///
///
///
public DbFind(string field, string tablename, string selectfield, string[] setValueField, string caller, string formname, string condition)
{
InitializeComponent();
StartPosition = FormStartPosition.CenterParent;
Field = 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;
}
}
protected override void WndProc(ref Message m)
{
//拦截双击标题栏、移动窗体的系统消息
if (m.Msg != 0xA3)
{
base.WndProc(ref m);
}
}
private void DbFind_Load(object sender, EventArgs e)
{
asc.controllInitializeSize(this);
//用来存放过滤的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.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)
{
if (e.KeyCode == Keys.Enter)
pagination1.BindDataToNavigator(DbFindGridView, BindTable, SelectField, "ID", Caller, BaseUtil.GetScreenSqlCondition(ctl).Replace("where", "") + ((Condition == "" || Condition == null) ? "" : " and " + Condition));
}
private void DbFindGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
try
{
//先判断DataTable里面是否有这个字段,然后从打开的窗口里面去获取到这个Form,从Form中的指定Panel获取到指定字段的控件
for (int i = 0; i < dt.Columns.Count; i++)
{
for (int j = 0; j < SetValueField.Length; j++)
{
if (SetValueField[j] == dt.Columns[i].Caption || SetValueField[j] == dt.Columns[i].ColumnName || dt.Columns[i].Caption.Contains(SetValueField[j]))
{
FormCollection fmCollection = Application.OpenForms;
fmCollection[FormName].Controls[SetValueField[j]].Text = DbFindGridView.Rows[e.RowIndex].Cells[dt.Columns[i].ColumnName].Value.ToString();
}
}
}
Dispose();
Close();
}
catch (Exception)
{
Dispose();
Close();
}
}
//按下Esc键的时候关闭当前的界面,用于DbFind
private void DbFindGridView_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Escape)
{
Dispose();
Close();
}
}
private void DbFind_SizeChanged(object sender, EventArgs e)
{
asc.controlAutoSize(this);
}
//列宽发生变化的时候TextBox的宽度也发生变化
private void DbFindGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
if (ctl != null)
ctl[DbFindGridView.Columns.IndexOf(e.Column)].Width = e.Column.Width;
}
}
}