using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace GreatWall.QueryFrm
{
/// <summary>
/// 用来生成查询以及过滤条件的窗体
/// </summary>
public class Query : System.Windows.Forms.Form
{
#region//系统定义的变量
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Panel panelMain;
#endregion
//***********************************************
private System.Drawing.Point startPoint; //起始绘制位置
private int X_CurrentPoint; //当前绘制的X坐标
private int Y_CurrentPoint; //当前绘制的Y坐标
private ArrayList comboxArrayList; //条件选择框的数组
private ArrayList valueArrayList; //选择条件值
private ArrayList opraterArrayList; //比较符号控件数组
private ArrayList continueArrayList; //关系选择COMBOBOX数组
private string[] TypeCodes; //存储当前表格字段类型
private int LineHeight; //定义每行占据的高度
private System.Data.DataTable dtComboxItems; //存储查询条件选择项目的Table;
private string strsqlcondition; //生成的sql语句
private bool IsRowFilter; //是否不需要表名
private bool IsCommandShortDataString; //是否需要短日期格式
private int OverFlowLineNumber; //垂直SCROLLBAR出现后新增加的条件行数
//***********************************************
#region//构造函数 窗体加载以及资源清理
public Query(DataGridTableStyle[] tableStyles,System.Data.DataTable[] Tables,bool IsRowFilter,bool IsCommandShortDataString)
{
InitializeComponent();
//窗体标题
if ( IsRowFilter )
{
this.Text = "数据过滤条件";
}
else
{
this.Text = "数据查询条件";
}
//初始化数据
this.InitedData(tableStyles,Tables,IsRowFilter,IsCommandShortDataString);
}
//第一次重载,接受数据集参数
public Query(DataGridTableStyle[] tableStyles,System.Data.DataSet ds,bool IsRowFilter,bool IsCommandShortDataString)
{
InitializeComponent();
//窗体标题
if ( IsRowFilter )
{
this.Text = "数据过滤条件";
}
else
{
this.Text = "数据查询条件";
}
//初始化数据
this.InitedData(tableStyles,ds,IsRowFilter,IsCommandShortDataString);
}
private void Query_Load(object sender, System.EventArgs e)
{
//绘制第一行
DrawingConditionControl(0);
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#endregion
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.panelMain = new System.Windows.Forms.Panel();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button1.Location = new System.Drawing.Point(102, 240);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "提交查询";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button2.Location = new System.Drawing.Point(210, 240);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(90, 23);
this.button2.TabIndex = 1;
this.button2.Text = "重新设置条件";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// panelMain
//
this.panelMain.Location = new System.Drawing.Point(6, 6);
this.panelMain.Name = "panelMain";
this.panelMain.Size = new System.Drawing.Size(540, 216);
this.panelMain.TabIndex = 2;
//
// button3
//
this.button3.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button3.Location = new System.Drawing.Point(342, 240);
this.button3.Name = "button3";
this.button3.TabIndex = 3;
this.button3.Text = "取消查询";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Query
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(556, 291);
this.Controls.Add(this.button3);
this.Controls.Add(this.panelMain);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Query";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "数据查询窗口";
this.Load += new System.EventHandler(this.Query_Load);
this.ResumeLayout(false);
}
#endregion
#region//初始化用户变量
private void InitedData(DataGridTableStyle[] tableStyles,System.Data.DataTable[] Tables,bool IsRowFilter,bool IsCommandShortDataString)
{
//初始化查询条件选择项 表
int TableCount = Tables.GetLength(0);
int TableStyleCount = tableStyles.GetLength(0);
int ColumnsCount = 0;
for ( int i = 0;i<TableStyleCount;i++ )
{
ColumnsCount += tableStyles[i].GridColumnStyles.Count;
}
this.TypeCodes = new string[ColumnsCount];
//创建条件表
//设置条件表的结构
this.dtComboxItems = new System.Data.DataTable();
System.Data.DataColumn columnValue = new System.Data.DataColumn("columnValue",typeof(System.String));
dtComboxItems.Columns.Add(columnValue);
dtComboxItems.PrimaryKey = new System.Data.DataColumn[]{columnValue};
System.Data.DataColumn columnText = new System.Data.DataColumn("columnText",typeof(System.String));
dtComboxItems.Columns.Add(columnText);
//创建选择项目数据记录;
for ( int i = 0;i<TableStyleCount;i++ )
{
for ( int j = 0;j<tableStyles[i].GridColumnStyles.Count;j++ )
{
string ItemValue = tableStyles[i].MappingName + "." +tableStyles[i].GridColumnStyles[j].MappingName;
System.Data.DataRow newRow = this.dtComboxItems.NewRow();
newRow["columnValue"] = ItemValue;
newRow["columnText"] = tableStyles[i].GridColumnStyles[j].HeaderText;
this.dtComboxItems.Rows.Add(newRow);
}
}
//设置类型数组
ColumnsCount = 0;
for ( int i = 0;i<TableCount;i++ )
{
for ( int j = 0;j<Tables[i].Columns.Count;j++ )
{
string TypeCodeString = Tables[i].TableName + "." + Tables[i].Columns[j].ColumnName;
System.Data.DataRow Row = this.dtComboxItems.Rows.Find(new System.String[]{TypeCodeString});
if ( this.dtComboxItems.Rows.Find(new System.String[]{TypeCodeString}) != null )
{
bool HasRecord = false;
for( int k =0;k<ColumnsCount;k++)
{
string objstring = this.TypeCodes[k];
if ( objstring!=null )
{
int index = objstring.IndexOf('|');
objstring = objstring.Remove(index,objstring.Length-index);
index = objstring.IndexOf('.') +1;
objstring = objstring.Substring(index,objstring.Length - index );
string RowValueString = Row["columnValue"].ToString();
index = RowValueString.IndexOf('.') + 1;
RowValueString = RowValueString.Substring(index,RowValueString.Length - index );
if ( objstring == RowValueString )
{
HasRecord = true;
break;
}
}
}
if ( HasRecord )
{
continue;
}
else
{
TypeCodes[ColumnsCount] = TypeCodeString + "|" + Tables[i].Columns[j].DataType.ToString();
ColumnsCount ++;
}
}
else
{
System.Data.DataRow RowTemp = null;
int indexTableStyleCurrent = 0;
while ( RowTemp == null )
{
TypeCodeString = tableStyles[indexTableStyleCurrent].MappingName + "." + Tables[i].Columns[j].ColumnName;
RowTemp = this.dtComboxItems.Rows.Find(new System.String[]{TypeCodeString});
if ( RowTemp != null )
{
bool HasRecord = false;
for( int k =0;k<ColumnsCount;k++)
{
string objstring = this.TypeCodes[k];
if ( objstring!=null )
{
int index = objstring.IndexOf('|');
objstring = objstring.Remove(index,objstring.Length-index);
index = objstring.IndexOf('.') +1;
objstring = objstring.Substring(index,objstring.Length - index );
string RowValueString = RowTemp["columnValue"].ToString();
index = RowValueString.IndexOf('.') + 1;
RowValueString = RowValueString.Substring(index,RowValueString.Length - index );
if ( objstring == RowValueString )
{
HasRecord = true;
break;
}
}
}
if ( HasRecord )
{
continue;
}
else
{
TypeCodes[ColumnsCount] = TypeCodeString + "|" + Tables[i].Columns[j].DataType.ToString();
ColumnsCount ++;
break;
}
}
else
{
if ( ++indexTableStyleCurrent == TableStyleCount )
{
break;
}
}
}
}
}
}
//去除当前Style中有但是表中不存在的字段值
System.Data.DataRow[] Rows = this.dtComboxItems.Select();
foreach ( System.Data.DataRow Row in Rows )
{
bool HasRecord = false;
for( int i =0;i<ColumnsCount;i++)
{
string objstring = this.TypeCodes[i];
int index = objstring.IndexOf('|');
if ( objstring.Remove(index,objstring.Length-index)== Row["columnValue"].ToString() )
{
HasRecord = true;
break;
}
}
if ( HasRecord )
{
continue;
}
else
{
this.dtComboxItems.Rows.Remove(Row);
}
}
//初始化查询条件选择项 表 结束
this.IsRowFilter = IsRowFilter;
this.IsCommandShortDataString = IsCommandShortDataString;
SetDefaultData();
}
//初始化用户变量的第一次重载
private void InitedData(DataGridTableStyle[] tableStyles,System.Data.DataSet ds,bool IsRowFilter,bool IsCommandShortDataString)
{
//初始化查询条件选择项 表
int TableCount = ds.Tables.Count;
int TableStyleCount = tableStyles.GetLength(0);
int ColumnsCount = 0;
for ( int i = 0;i<TableStyleCount;i++ )
{
ColumnsCount += tableStyles[i].GridColumnStyles.Count;
}
this.TypeCodes = new string[ColumnsCount];
//创建条件表
//设置条件表的结构
this.dtComboxItems = new System.Data.DataTable();
System.Data.DataColumn columnValue = new System.Data.DataColumn("columnValue",typeof(System.String));
dtComboxItems.Columns.Add(columnValue);
dtComboxItems.PrimaryKey = new System.Data.DataColumn[]{columnValue};
System.Data.DataColumn columnText = new System.Data.DataColumn("columnText",typeof(System.String));
dtComboxItems.Columns.Add(columnText);
//创建选择项目数据记录;
for ( int i = 0;i<TableStyleCount;i++ )
{
for ( int j = 0;j<tableStyles[i].GridColumnStyles.Count;j++ )
{
string ItemValue = tableStyles[i].MappingName + "." +tableStyles[i].GridColumnStyles[j].MappingName;
System.Data.DataRow newRow = this.dtComboxItems.NewRow();
newRow["columnValue"] = ItemValue;
newRow["columnText"] = tableStyles[i].GridColumnStyles[j].HeaderText;
this.dtComboxItems.Rows.Add(newRow);
}
}
//设置类型数组
ColumnsCount = 0;
for ( int i = 0;i<TableCount;i++ )
{
for ( int j = 0;j<ds.Tables[i].Columns.Count;j++ )
{
string TypeCodeString = ds.Tables[i].TableName + "." + ds.Tables[i].Columns[j].ColumnName;
System.Data.DataRow Row = this.dtComboxItems.Rows.Find(new System.String[]{TypeCodeString});
if ( this.dtComboxItems.Rows.Find(new System.String[]{TypeCodeString}) != null )
{
bool HasRecord = false;
for( int k =0;k<ColumnsCount;k++)
{
string objstring = this.TypeCodes[k];
if ( objstring!=null )
{
int index = objstring.IndexOf('|');
objstring = objstring.Remove(index,objstring.Length-index);
index = objstring.IndexOf('.') +1;
objstring = objstring.Substring(index,objstring.Length - index );
string RowValueString = Row["columnValue"].ToString();
index = RowValueString.IndexOf('.') + 1;
RowValueString = RowValueString.Substring(index,RowValueString.Length - index );
if ( objstring == RowValueString )
{
HasRecord = true;
break;
}
}
}
if ( HasRecord )
{
continue;
}
else
{
TypeCodes[ColumnsCount] = TypeCodeString + "|" + ds.Tables[i].Columns[j].DataType.ToString();
ColumnsCount ++;
}
}
else
{
System.Data.DataRow RowTemp = null;
int indexTableStyleCurrent = 0;
while ( RowTemp == null )
{
TypeCodeString = tableStyles[indexTableStyleCurrent].MappingName + "." + ds.Tables[i].Columns[j].ColumnName;
RowTemp = this.dtComboxItems.Rows.Find(new System.String[]{TypeCodeString});
if ( RowTemp != null )
{
bool HasRecord = false;
for( int k =0;k<ColumnsCount;k++)
{
string objstring = this.TypeCodes[k];
if ( objstring!=null )
{
int index = objstring.IndexOf('|');
objstring = objstring.Remove(index,objstring.Length-index);
index = objstring.IndexOf('.') +1;
objstring = objstring.Substring(index,objstring.Length - index );
string RowValueString = RowTemp["columnValue"].ToString();
index = RowValueString.IndexOf('.') + 1;
RowValueString = RowValueString.Substring(index,RowValueString.Length - index );
if ( objstring == RowValueString )
{
HasRecord = true;
break;
}
}
}
if ( HasRecord )
{
continue;
}
else
{
TypeCodes[ColumnsCount] = TypeCodeString + "|" + ds.Tables[i].Columns[j].DataType.ToString();
ColumnsCount ++;
break;
}
}
else
{
if ( ++indexTableStyleCurrent == TableStyleCount )
{
break;
}
}
}
}
}
}
//去除当前Style中有但是表中不存在的字段值
System.Data.DataRow[] Rows = this.dtComboxItems.Select();
foreach ( System.Data.DataRow Row in Rows )
{
bool HasRecord = false;
for( int i =0;i<ColumnsCount;i++)
{
string objstring = this.TypeCodes[i];
int index = objstring.IndexOf('|');
if ( objstring.Remove(index,objstring.Length-index)== Row["columnValue"].ToString() )
{
HasRecord = true;
break;
}
}
if ( HasRecord )
{
continue;
}
else
{
this.dtComboxItems.Rows.Remove(Row);
}
}
//初始化查询条件选择项 表 结束
this.IsRowFilter = IsRowFilter;
this.IsCommandShortDataString = IsCommandShortDataString;
SetDefaultData();
}
private void SetDefaultData()
{
this.OverFlowLineNumber = 0;
this.LineHeight = 30;
this.startPoint = new Point(20,20);
this.X_CurrentPoint = this.startPoint.X;
this.Y_CurrentPoint = this.startPoint.Y;
this.comboxArrayList = new ArrayList();
this.valueArrayList = new ArrayList();
this.opraterArrayList = new ArrayList();
this.continueArrayList = new ArrayList();
}
#endregion
#region//在Panel上绘制的过程
private void DrawingConditionControl(int ControlIndex)
{
DrwaingComboxCodition(ControlIndex);
DrwaingOpraterConditon(ControlIndex,false);
DrwaingValueConditon(ControlIndex,false);
DrwaingContinueCondition(ControlIndex);
}
private void DrwaingComboxCodition(int ControlIndex)
{
ComboBox combox = new ComboBox();
combox.DropDownStyle = ComboBoxStyle.DropDownList;
combox.Name = "combox_" + ControlIndex.ToString();
this.X_CurrentPoint = this.panelMain.Location.X + this.startPoint.X;
this.Y_CurrentPoint = this.LineHeight * ControlIndex + this.panelMain.Location.Y + this.startPoint.Y;
//上移制定距离,补充位置差,
if ( this.OverFlowLineNumber >= 1 )
{
this.Y_CurrentPoint -= (this.OverFlowLineNumber - 1 )*this.LineHeight + this.LineHeight/3;
}
combox.Location = new Point(this.X_CurrentPoint,this.Y_CurrentPoint);
combox.Width = 120;
this.panelMain.Controls.Add(combox);
comboxArrayList.Add(combox);
this.X_CurrentPoint += combox.Width + 10;
combox.SelectionChangeCommitted += new EventHandler(combox_SelectionChangeCommitted);
//绑定选择条目
combox.DataSource = this.dtComboxItems.Copy();
combox.DisplayMember = "columnText";
combox.ValueMember = "columnValue";
if ( combox.Items.Count > 0 && ControlIndex<combox.Items.Count )
{
combox.SelectedIndex = ControlIndex;
}
}
#endregion
#region//字段选择项目变化相应事件
private void combox_SelectionChangeCommitted(Object sender,System.EventArgs e)
{
ComboBox combox = (ComboBox)sender;
string strName = combox.Name;
int index = strName.IndexOf('_') + 1;
int ControlIndex = Convert.ToInt32(strName.Substring(index,strName.Length - index));
Control control = (Control)valueArrayList[ControlIndex];
if ( control!=null )
{
control.Dispose();
}
this.X_CurrentPoint = combox.Location.X + 213;
this.Y_CurrentPoint = combox.Location.Y;
//重绘部分关联控件true指示重绘,如果为false则新建
DrwaingOpraterConditon(ControlIndex,true);
DrwaingValueConditon(ControlIndex,true);
}
#endregion
#region//绘制操作符号
private void DrwaingOpraterConditon(int ControlIndex,bool HasControl)
{
ComboBox combox = (ComboBox)comboxArrayList[ControlIndex];
string ColumnName = combox.SelectedValue.ToString();
string DataTypeName = string.Empty;
foreach ( string str in this.TypeCodes )
{
if ( str == null )
{
continue;
}
else
{
if (str.IndexOf(ColumnName + "|" )!=-1 )
{
DataTypeName = str.Replace(ColumnName + "|","");
break;
}
}
}
//捕获异常
if ( DataTypeName == string.Empty )
{
throw(new Exception("你所设置的条件[" + DataTypeName + "]不能在参数Tables中找到自己对应的数据类型!"));
}
ComboBox comboxOprater = new ComboBox();
comboxOprater.DropDownStyle = ComboBoxStyle.DropDownList;
comboxOprater.Name = "oprater_" + ControlIndex.ToString();
comboxOprater.Width = 80;
if ( !HasControl )
{
comboxOprater.Location = new Point(this.X_CurrentPoint,this.Y_CurrentPoint);
this.panelMain.Controls.Add(comboxOprater);
opraterArrayList.Add(comboxOprater);
this.X_CurrentPoint += comboxOprater.Width + 10;
}
else
{
Control control = (Control)opraterArrayList[ControlIndex];
comboxOprater.Location = control.Location;
if ( control!=null )
{
control.Dispose();
}
this.panelMain.Controls.Add(comboxOprater);
opraterArrayList[ControlIndex] = comboxOprater;
}
switch ( DataTypeName )
{
case "System.Boolean":
case "System.Byte":
{
comboxOprater.Items.Clear();
comboxOprater.Items.Add("等于");
break;
}
case "System.Decimal":
case "System.Double":
case "System.Int16":
case "System.Int32":
{
comboxOprater.Items.Clear();
comboxOprater.Items.Add("等于");
comboxOprater.Items.Add("小于");
comboxOprater.Items.Add("小于等于");
comboxOprater.Items.Add("大于");
comboxOprater.Items.Add("大于等于");
break;
}
case "System.DateTime":
{
comboxOprater.Items.Clear();
comboxOprater.Items.Add("等于");
comboxOprater.Items.Add("小于");
comboxOprater.Items.Add("小于等于");
comboxOprater.Items.Add("大于");
comboxOprater.Items.Add("大于等于");
break;
}
case "System.String":
{
comboxOprater.Items.Clear();
comboxOprater.Items.Add("等于");
comboxOprater.Items.Add("相似");
break;
}
default:
{
comboxOprater.Items.Clear();
comboxOprater.Items.Add("等于");
comboxOprater.Items.Add("小于");
comboxOprater.Items.Add("小于等于");
comboxOprater.Items.Add("大于");
comboxOprater.Items.Add("大于等于");
comboxOprater.Items.Add("相似");
break;
}
}
comboxOprater.SelectedIndex = 0;
}
#endregion
#region//绘制条件输入控件
private void DrwaingValueConditon(int ControlIndex,bool HasContol)
{
ComboBox combox = (ComboBox)comboxArrayList[ControlIndex];
string ColumnName = combox.SelectedValue + "|";
string DataTypeName = System.TypeCode.String.ToString();
foreach ( string str in this.TypeCodes )
{
if (str.IndexOf(ColumnName )!=-1 )
{
DataTypeName = str.Replace(ColumnName,"");
break;
}
}
if ( !HasContol )
{
this.X_CurrentPoint += 7;
}
switch ( DataTypeName )
{
case "System.Boolean":
case "System.Byte":
{
GroupBox groupbox = new GroupBox();
groupbox.Name = "value_" + ControlIndex.ToString();
groupbox.Location = new Point(this.X_CurrentPoint,this.Y_CurrentPoint-10);
groupbox.Width = 120;
groupbox.Height = this.LineHeight + 2;
groupbox.Text = string.Empty;
this.panelMain.Controls.Add(groupbox);
if ( !HasContol )
{
valueArrayList.Add(groupbox);
}
else
{
Control SelectCombox = (Control)comboxArrayList[ControlIndex];
Control control = (Control)valueArrayList[ControlIndex];
groupbox.Location = new Point(control.Location.X,SelectCombox.Location.Y-7);
control.Dispose();
valueArrayList[ControlIndex] = groupbox;
}
RadioButton radioBox1= new RadioButton();
radioBox1.Name = "radioBox_" + ControlIndex.ToString() + "_1";
radioBox1.Text = "是";
radioBox1.Location = new Point(3,7);
radioBox1.Width = 58;
radioBox1.Checked = true;
RadioButton radioBox2= new RadioButton();
radioBox2.Name = "radioBox_" + ControlIndex.ToString() + "_2";
radioBox2.Text = "否";
radioBox2.Location = new Point(60,7);
radioBox2.Width = 55;
groupbox.Controls.Add(radioBox1);
groupbox.Controls.Add(radioBox2);
break;
}
case "System.Decimal":
case "System.Double":
case "System.Int16":
case "System.Int32":
{
NumericUpDown numericUpDown = new NumericUpDown();
numericUpDown.Name = "value_" + ControlIndex.ToString();
numericUpDown.Maximum = 10000000;
numericUpDown.Minimum = -10000000;
numericUpDown.Value = 0;
numericUpDown.Location = new Point(this.X_CurrentPoint,this.Y_CurrentPoint);
numericUpDown.Width = 120;
this.panelMain.Controls.Add(numericUpDown);
if ( !HasContol )
{
valueArrayList.Add(numericUpDown);
}
else
{
Control SelectCombox = (Control)comboxArrayList[ControlIndex];
Control control = (Control)valueArrayList[ControlIndex];
numericUpDown.Location = new Point(control.Location.X,SelectCombox.Location.Y);
control.Dispose();
valueArrayList[ControlIndex] = numericUpDown;
}
break;
}
case "System.DateTime":
{
DateTimePicker dateTimePicker1 = new DateTimePicker();
dateTimePicker1.Name = "value_" + ControlIndex.ToString();
dateTimePicker1.Location = new Point(this.X_CurrentPoint,this.Y_CurrentPoint);
dateTimePicker1.Width = 120;
dateTimePicker1.Value = DateTime.Now;
this.panelMain.Controls.Add(dateTimePicker1);
if ( !HasContol )
{
valueArrayList.Add(dateTimePicker1);
}
else
{
Control SelectCombox = (Control)comboxArrayList[ControlIndex];
Control control = (Control)valueArrayList[ControlIndex];
dateTimePicker1.Location = new Point(control.Location.X,SelectCombox.Location.Y);
control.Dispose();
valueArrayList[ControlIndex] = dateTimePicker1;
}
break;
}
case "System.String":
default:
{
TextBox textBox = new TextBox();
textBox.Name = "value_" + ControlIndex.ToString();
textBox.Text = string.Empty;
textBox.Location = new Point(this.X_CurrentPoint,this.Y_CurrentPoint);
textBox.Width = 120;
this.panelMain.Controls.Add(textBox);
if ( !HasContol )
{
valueArrayList.Add(textBox);
}
else
{
Control SelectCombox = (Control)comboxArrayList[ControlIndex];
Control control = (Control)valueArrayList[ControlIndex];
textBox.Location = new Point(control.Location.X,SelectCombox.Location.Y);
control.Dispose();
valueArrayList[ControlIndex] = textBox;
}
break;
}
}
}
#endregion
#region//绘制条件选择下拉列表
private void DrwaingContinueCondition(int ControlIndex)
{
ComboBox combox = new ComboBox();
combox.DropDownStyle = ComboBoxStyle.DropDownList;
combox.Name = "continue_" + ControlIndex.ToString();
combox.Items.Add("选择");
combox.Items.Add("而且");
combox.Items.Add("或者");
combox.SelectedIndex = 0;
Control SelectCombox = (Control)comboxArrayList[ControlIndex];
combox.Location = new Point(SelectCombox.Location.X + 367,SelectCombox.Location.Y);
combox.Width = 60;
this.panelMain.Controls.Add(combox);
continueArrayList.Add(combox);
combox.SelectedIndexChanged += new EventHandler(Continue_SelectedIndexChanged);
}
#endregion
#region//条件选择事件响应方法
protected void Continue_SelectedIndexChanged(object sender,System.EventArgs e)
{
ComboBox combox = (ComboBox)sender;
if ( combox.SelectedIndex == 0 )
{
return;
}
string strName = combox.Name;
int index = strName.IndexOf('_') +1;
int ControlIndex = Convert.ToInt32(strName.Substring(index,strName.Length - index)) + 1;
combox.Enabled = false;
DrawingConditionControl(ControlIndex);
if ( this.Y_CurrentPoint + this.LineHeight >= this.panelMain.Height )
{
if ( !this.panelMain.AutoScroll)
{
this.panelMain.AutoScroll = true;
}
this.OverFlowLineNumber ++ ;
this.panelMain.AutoScrollPosition = new Point(0,this.Bottom);
}
}
#endregion
#region//循环遍历字段条件生成SQL语句 false返回带表名的sql语句,true则返回不带表名的
private string CreatSqlCondition()
{
int ComboxCount = comboxArrayList.Count;
System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
//关系符号
string txtContinue = string.Empty;
for (int i=0;i<ComboxCount;i++ )
{
string ColumnName = ((ComboBox)comboxArrayList[i]).SelectedValue.ToString();
//获取数据类型
string DataTypeName = string.Empty;
string txtConditon = string.Empty;
foreach ( string str in this.TypeCodes )
{
if (str.IndexOf(ColumnName + "|" )!=-1 )
{
DataTypeName = str.Replace(ColumnName + "|","");
break;
}
}
//捕获异常
if ( DataTypeName == string.Empty )
{
throw(new Exception("你所设置的条件[" + DataTypeName + "]不能在参数Tables中找到自己对应的数据类型!"));
}
bool IsAddDot = true;
System.TypeCode tyCode;
switch ( DataTypeName )
{
case "System.Boolean":
case "System.Byte":
{
tyCode = System.TypeCode.Boolean;
IsAddDot = false;
break;
}
case "System.Decimal":
case "System.Double":
case "System.Int16":
case "System.Int32":
{
tyCode = System.TypeCode.Decimal;
IsAddDot = false;
break;
}
case "System.DateTime":
{
tyCode = System.TypeCode.DateTime;
IsAddDot = true;
break;
}
default:
{
tyCode = System.TypeCode.String;
IsAddDot = true;
break;
}
}
string txt = string.Empty;
if ( tyCode == System.TypeCode.String )
{
txt = ((TextBox)valueArrayList[i]).Text;
}
else if ( tyCode == System.TypeCode.Boolean )
{
GroupBox groupbox = (GroupBox)valueArrayList[i];
RadioButton radioButton1 = (RadioButton)groupbox.Controls[0];
RadioButton radioButton2 = (RadioButton)groupbox.Controls[1];
if ( !radioButton1.Checked && radioButton2.Checked )
{
txt = "0";
}
else if ( radioButton1.Checked && !radioButton2.Checked )
{
txt = "1";
}
}
else if ( tyCode == System.TypeCode.Decimal )
{
txt = ((NumericUpDown)valueArrayList[i]).Value.ToString();
}
else if ( tyCode == System.TypeCode.DateTime )
{
if ( this.IsCommandShortDataString )
{
txt = ((DateTimePicker)valueArrayList[i]).Value.ToShortDateString();
}
else
{
txt = ((DateTimePicker)valueArrayList[i]).Value.ToString();
}
}
if ( txt == string.Empty )
{
continue;
}
string txtOprater = ((ComboBox)opraterArrayList[i]).SelectedItem.ToString();
txtOprater = this.Oprater(txtOprater);
if ( txt.Length > 0 )
{
txtContinue = ((ComboBox)continueArrayList[i]).SelectedItem.ToString();
txtContinue = ContinueTxt(txtContinue);
}
if ( this.IsRowFilter )
{
ColumnName = ColumnName.Replace(ColumnName.Substring(0,ColumnName.IndexOf('.')+1),"");
}
SqlStringCondition( ColumnName, txt,txtContinue,txtOprater,strBuilder, IsAddDot);
}
//添加and字符串连接多表查询条件
string sqlstring = strBuilder.ToString();
if ( !sqlstring.EndsWith(")") )
{
sqlstring = sqlstring.Substring(0,sqlstring.LastIndexOf(')')+1 );
}
return sqlstring;
}
#endregion
#region//生成相应字段的SQL语句并插入
private void SqlStringCondition(string ColumnName,string txt, string txtContinue,string txtOprater,System.Text.StringBuilder strBuilder,bool IsAddDot )
{
if ( strBuilder.Length == 0 )
{
if ( txtOprater == "Like" )
{
strBuilder.Append(" (");
strBuilder.Append(ColumnName);
strBuilder.Append(" ");
strBuilder.Append(txtOprater);
if ( IsAddDot )
{
strBuilder.Append(" \'*");
}
else
{
strBuilder.Append(" *");
}
strBuilder.Append(txt);
if ( IsAddDot )
{
strBuilder.Append("*\' ) ");
}
else
{
strBuilder.Append("* ) ");
}
}
else
{
strBuilder.Append(" (");
strBuilder.Append(ColumnName);
strBuilder.Append(" ");
strBuilder.Append(txtOprater);
if ( IsAddDot )
{
strBuilder.Append(" \'");
}
else
{
strBuilder.Append(" ");
}
strBuilder.Append(txt);
if ( IsAddDot )
{
strBuilder.Append("\' ) ");
}
else
{
strBuilder.Append(" ) ");
}
}
}
else
{
if ( txtOprater == "Like" )
{
strBuilder.Append(" (");
strBuilder.Append(ColumnName);
strBuilder.Append(" ");
strBuilder.Append(txtOprater);
if ( IsAddDot )
{
strBuilder.Append(" \'*");
}
else
{
strBuilder.Append(" *");
}
strBuilder.Append(txt);
if ( IsAddDot )
{
strBuilder.Append("*\' ) ");
}
else
{
strBuilder.Append("* ) ");
}
}
else
{
strBuilder.Append(" (");
strBuilder.Append(ColumnName);
strBuilder.Append(" ");
strBuilder.Append(txtOprater);
if ( IsAddDot )
{
strBuilder.Append(" \'");
}
else
{
strBuilder.Append(" ");
}
strBuilder.Append(txt);
if ( IsAddDot )
{
strBuilder.Append("\' ) ");
}
else
{
strBuilder.Append(" ) ");
}
}
}
strBuilder.Append(" ");
strBuilder.Append(txtContinue);
}
#endregion
#region//符号转换
private string ContinueTxt(string Txt)
{
string result = string.Empty;
switch (Txt)
{
case "而且":
{
result = "And";
break;
}
case "或者":
{
result = "Or";
break;
}
}
return result;
}
private string Oprater(string Txt)
{
string result = string.Empty;
switch (Txt)
{
case "等于":
{
result = "=";
break;
}
case "小于":
{
result = "<";
break;
}
case "大于":
{
result = ">";
break;
}
case "小于等于":
{
result = "<=";
break;
}
case "大于等于":
{
result = ">=";
break;
}
case "相似":
{
result = "Like";
break;
}
}
return result;
}
#endregion
#region//注入过滤(未完成)
// private bool CheckParams(paramsobject[]args)
// {
// string[] Lawlesses={"=","'"};
// if(Lawlesses==null||Lawlesses.Length<=0)
// {
// return true;
// }
// //构造正则表达式,例:Lawlesses是=号和'号,则正则表达式为.*[=}'].* (正则表达式相关内容请见MSDN)
// //另外,由于我是想做通用而且容易修改的函数,所以多了一步由字符数组到正则表达式,实际使用中,直接写正则表达式亦可;
// string str_Regex=".*[";
// for(inti=0;i<Lawlesses.Length-1;i++)
// str_Regex+=Lawlesses[i]+"|";
// str_Regex += Lawlesses[Lawlesses.Length-1]+"].*";
// //
// foreach(objectarg in args)
// {
// if(argisstring)//如果是字符串,直接检查
// {
// if(Regex.Matches(arg.ToString(),str_Regex).Count>0)
// returnfalse;
// }
// else if(arg is ICollection)//如果是一个集合,则检查集合内元素是否字符串,是字符串,就进行检查
// {
// foreach(objectobj in (ICollection)arg)
// {
// if(objisstring)
// {
// if(Regex.Matches(obj.ToString(),str_Regex).Count>0)
// return false;
// }
// }
// }
// }
// return true;
// }
#endregion
#region//恢复窗体默认值以及界面
private void SetDefaultFace()
{
if ( this.CreatSqlCondition() != string.Empty )
{
DialogResult result = System.Windows.Forms.MessageBox.Show("这个操作会取消你当前设置的查询条件!\r你确定要这么做吗?","提示",System.Windows.Forms.MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1);
if ( result == System.Windows.Forms.DialogResult.No )
{
return;
}
}
this.panelMain.Controls.Clear();
this.SetDefaultData();
this.DrawingConditionControl(0);
}
#endregion
#region//按钮事件
//恢复查询条件默认值
private void button2_Click(object sender, System.EventArgs e)
{
this.SetDefaultFace();
}
//确认查询条件
private void button1_Click(object sender, System.EventArgs e)
{
this.strsqlcondition = this.CreatSqlCondition();
this.DialogResult = DialogResult.OK;
MessageBox.Show(this.strsqlcondition);
this.Close();
}
//取消查询
private void button3_Click(object sender, System.EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
#endregion
#region //属性
public string AStrSqlCondtion
{
get
{
return strsqlcondition;
}
}
#endregion
}
}
浙公网安备 33010602011771号