原创企业级控件库之组合查询控件

原创企业级控件库之组合查询控件

发布日期:2010年12月10日星期五 作者:EricHu

  无论对于企业还是对于软件开发者来说,拥有自己的一套常用控件库,对于开发产品来说不仅可以缩短开发时间,提高开发效率,同时对一个企业整个产品的形象也会大大提高。本系列控件为作者在实际开发应用中总结出来,且成功应用于多个项目。现对整个控件库一一讲解,最后我会把整个控件库开源,方便你的使用,同时会给一个综合应用这些控件的实例。

  成就别人、等于成就自己。我没什么要求,欢迎大家多多支持与评论,觉得不错的,记得点击文章左下角的”关注博客”,就这么简单。

  整个控件样式如下:

  说明:

  1、本控件分为组合查询与固定查询两种,上图为组合查询。固定查询见下图。

  2、组合查询以下几部份组成:

a. 查询项:设置查询的项目,通俗点就是表中的字段。

b. 运算符:设置查询条件,如:等于、大小、包含、为空等。

c. 值:设置查询的值。

d. 组合方式:当查询条件大于两个时,其组合条件可设为:与方式、或方式、非方式三种。

e. 新增:新增查询条件到组合条件框。

f. 清除:当输入错误时,可以清除组合条件框中的组合查询条件。

g. 查询:当用户单击查询时,返回查询表达式,供用户使用。

h. 固定查询:当用户单击固定查询时,控件变成固定查询控件,同时,固定查询变成“组合查询”,如下图:  

  3、本控件特点:

a. 对用户输入的危险字符进行了屏蔽,可有效防止对数据库的破坏。

b. 控件返回的查询条件都是合法的Sql语句中的Where条件表达式,可以直接使用。

c. 开放源代码,用户可以根据自己的需要进行定制,消除你的后顾之优。

  4、本控件类图如下所示:

5、本控件核心代码

a. 得到组合查询表达式

代码
 
  
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #region 组合查询表达式
/// <summary>
/// 单击[查询]按钮时发生
/// </summary>
[Category( " 组合查询 " ), Description( " 单击[查询]按钮时发生。 " ), Browsable( true )]
public event EventHandler OnQueryClicked;

private string _queryExpression;
/// <summary>
/// 最终的查询表达式,可直接用于Where子句中
/// </summary>
[Category( " 组合查询 " ), Description( " 最终的查询表达式,可直接用于Where子句中。 " ), Browsable( false )]
public string QueryExpression
{
get { return _queryExpression; }
set
{
_queryExpression
= value;
if (OnQueryClicked != null )
{
OnQueryClicked(
this , null );
}
}
}
#endregion

b. 设置查询项中要显示的数据列表

代码
 
  
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #region 查询项相关控制

#region 设置查询项中要显示的数据列表
/// <summary>
/// 设置查询项中要显示的数据列表(推荐使用这个方法)
/// </summary>
/// <param name="dicListQueryItems"> 表示键和值的集合(键:数据字段,值:数据字段对应的数据类型) </param>
public void SetQueryItems(Dictionary < string , string > dicListQueryItems)
{
cboQueryItems.Items.Clear();
dicQueryItem
= null ;
dicQueryItem
= dicListQueryItems;
foreach (KeyValuePair < string , string > kvp in dicListQueryItems)
{
cboQueryItems.Items.Add(kvp.Key);
}

if (cboQueryItems.Items.Contains( " 案卷号 " )) // 把案卷号显示在第一个
{
cboQueryItems.Items.Remove(
" 案卷号 " );
cboQueryItems.Items.Insert(
0 , " 案卷号 " );
}
cboQueryItems.SelectedIndex
= 0 ;
}

/// <summary>
/// 设置查询项中要显示的数据列表
/// </summary>
/// <param name="sQueryItems"> string类型数组 </param>
public void SetQueryItems( string [] sQueryItems)
{
cboQueryItems.Items.Clear();

foreach ( string queryItem in sQueryItems)
{
cboQueryItems.Items.Add(queryItem);
}
cboQueryItems.SelectedIndex
= 0 ;
}

/// <summary>
/// 设置查询项中要显示的数据列表
/// </summary>
/// <param name="listQueryItems"> List泛型 </param>
public void SetQueryItems(List < string > listQueryItems)
{
cboQueryItems.Items.Clear();
foreach ( string queryItem in listQueryItems)
{
cboQueryItems.Items.Add(queryItem);
}
cboQueryItems.SelectedIndex
= 0 ;
}
#endregion

/// <summary>
/// 设置查询项的选择索引项
/// </summary>
/// <param name="index"> 索引的下标 </param>
public void SetQueryItemsSelectIndex( int index)
{
this .cboQueryItems.SelectedIndex = index;
}

/// <summary>
/// 设置查询项的选择内容
/// </summary>
/// <param name="sTxt"> 查询项选中的内容 </param>
public void SetQueryItemSelectText( string sTxt)
{
this .cboQueryItems.SelectedText = sTxt;
}

/// <summary>
/// 清空选项值
/// </summary>
public void ClearQueryItems()
{
cboQueryItems.Items.Clear();
}
#endregion

C.增加查询条件

代码
 
  
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #region 增加查询条件单击事件 btnAddQueryCondition_Click(object sender, EventArgs e)

private void btnAddQueryCondition_Click( object sender, EventArgs e)
{
if (cboQueryItems.Items.Count == 0 )
{
DialogHelper.ShowErrorMsg(
" 查询项为空,不能进行查询! " );
return ;
}
else
{
string sQueryItem = cboQueryItems.Text.Trim(); // 要查询的项
string sQueryValue = txtQueryValue.Text.Trim(); // 查询项的值
string sCombinMode = string .Empty; // 组合方式
string sQueryExpress = string .Empty; // 查询条件表达式

if (cboOperator.Text != " 为空 " && cboOperator.Text != " 不为空 " )
{
if ( string .IsNullOrEmpty(txtQueryValue.Text.Trim()))
{
DialogHelper.ShowWarningMsg(
" 必须输入查询项的值! " );
txtQueryValue.Focus();
return ;
}
else
{
if (StringHelper.HasDangerousWord(txtQueryValue.Text.Trim()))
{
DialogHelper.ShowWarningMsg(
" 对不起,你的输入含有危险字符,请重新输入! " );
txtQueryValue.Clear();
txtQueryValue.Focus();
return ;
}
}
}

switch (cboCombinMode.Text)
{
case " 与方式 " :
sCombinMode
= " AND " ;
break ;
case " 或方式 " :
sCombinMode
= " OR " ;
break ;
case " 非方式 " :
sCombinMode
= " AND NOT " ;
break ;
default :
break ;
}
#region 条件设置
switch (cboOperator.Text)
{
case " 包含 " :
sQueryExpress
= sQueryItem + " LIKE '% " + sQueryValue + " %' " ;
break ;
case " 左包含 " :
sQueryExpress
= sQueryItem + " LIKE ' " + sQueryValue + " %' " ;
break ;
case " 右包含 " :
sQueryExpress
= sQueryItem + " LIKE '% " + sQueryValue + " ' " ;
break ;
case " 为空 " :
sQueryExpress
= sQueryItem + " IS NULL OR " + sQueryItem + " = '' " ;
break ;
case " 不为空 " :
sQueryExpress
= sQueryItem + " IS NOT NULL And " + sQueryItem + " != '' " ;
break ;
case " 大于 " :
if (dicQueryItem.Count == 0 )
{
sQueryExpress
= sQueryItem + " > " + sQueryValue;
}
else
{
foreach (KeyValuePair < string , string > kvp in dicQueryItem)
{
if (cboQueryItems.Text == kvp.Key)
{
if ((kvp.Value == " System.DateTime " || kvp.Value == " System.String " )
&& kvp.Key != " 案卷号 " ) // kvp.Key != "案卷号"主是要因为案卷号是字符型,案卷号>'90',得不到正确结果,所以在此这样做
{
sQueryExpress
= sQueryItem + " > ' " + sQueryValue + " ' " ;
}
else
{
sQueryExpress
= sQueryItem + " > " + sQueryValue;
}
}
}
}
break ;
case " 大于或等于 " :
if (dicQueryItem.Count == 0 )
{
sQueryExpress
= sQueryItem + " >= " + sQueryValue;
}
else
{
foreach (KeyValuePair < string , string > kvp in dicQueryItem)
{
if (cboQueryItems.Text == kvp.Key)
{
if ((kvp.Value == " System.DateTime " || kvp.Value == " System.String " ) && kvp.Key != " 案卷号 " )
{
sQueryExpress
= sQueryItem + " >= ' " + sQueryValue + " ' " ;
}
else
{
sQueryExpress
= sQueryItem + " >= " + sQueryValue;
}
}
}
}
break ;
case " 等于 " :
if (dicQueryItem.Count == 0 )
{
sQueryExpress
= sQueryItem + " = " + sQueryValue;
}
else
{
foreach (KeyValuePair < string , string > kvp in dicQueryItem)
{
if (cboQueryItems.Text == kvp.Key)
{
// ---3
if (kvp.Value == " System.DateTime " || kvp.Value == " System.String " )
{
sQueryExpress
= sQueryItem + " = ' " + sQueryValue + " ' " ;
}
else
{
sQueryExpress
= sQueryItem + " = " + sQueryValue;
}

}
}
}
break ;
case " 小于或等于 " :
if (dicQueryItem.Count == 0 )
{
sQueryExpress
= sQueryItem + " <= " + sQueryValue;
}
else
{
foreach (KeyValuePair < string , string > kvp in dicQueryItem)
{
if (cboQueryItems.Text == kvp.Key)
{
if ((kvp.Value == " System.DateTime " || kvp.Value == " System.String " ) && kvp.Key != " 案卷号 " )
{
sQueryExpress
= sQueryItem + " <= ' " + sQueryValue + " ' " ;
}
else
{
sQueryExpress
= sQueryItem + " <= " + sQueryValue;
}
}
}
}
break ;
case " 小于 " :
if (dicQueryItem.Count == 0 )
{
sQueryExpress
= sQueryItem + " < " + sQueryValue;
}
else
{
foreach (KeyValuePair < string , string > kvp in dicQueryItem)
{
if (cboQueryItems.Text == kvp.Key)
{
if ((kvp.Value == " System.DateTime " || kvp.Value == " System.String " ) && kvp.Key != " 案卷号 " )
{
sQueryExpress
= sQueryItem + " < ' " + sQueryValue + " ' " ;
}
else
{
sQueryExpress
= sQueryItem + " < " + sQueryValue;
}
}
}
}
break ;
default :
break ;
}

if ( ! string .IsNullOrEmpty(txtQueryCondition.Text.Trim()))
{
sQueryExpress
= sCombinMode + " " + sQueryExpress;
}

txtQueryCondition.AppendText(
" " + sQueryExpress);
#endregion
}
}
#endregion

6、控件编译后,你可把编译后的dll文件直接拖运到Microsoft Visual Studi工具箱中,就可以看到我们的组合查询控件了。在实际应用中,仅需以下两个步骤即可完成整个代码。

a.在窗体社会分配加载时绑定相应的查询项

代码
 
  
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> /// <summary>
/// 增加测试数据
/// </summary>
private void BindTestData()
{
dtTest
= new DataTable();
dtTest.Columns.Add(
new DataColumn( " 身份证号 " , typeof (System.String)));
dtTest.Columns.Add(
new DataColumn( " 姓名 " , typeof (System.String)));
dtTest.Columns.Add(
new DataColumn( " QQ " , typeof (System.String)));
dtTest.Columns.Add(
new DataColumn( " 年龄 " , typeof (System.Int16)));
dtTest.Columns.Add(
new DataColumn( " 出生时间 " , typeof (System.DateTime)));
}

private void FrmUcCombinQueryTest_Shown( object sender, EventArgs e)
{
BindTestData();

// 绑定查询项
Dictionary < string , string > dicListQueryItems = new Dictionary < string , string > ();
foreach (DataColumn dc in dtTest.Columns)
{
dicListQueryItems.Add(dc.ColumnName, dc.DataType.ToString());
}
ucCombinQueryTest.SetQueryItems(dicListQueryItems);
}

b.单击查询时,返回给用户的查询表达式,用户直接得到使用,在组合查询的OnQueryClicked事件代码中进行处理。

 
 
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> private void ucCombinQueryTest_OnQueryClicked( object sender, EventArgs e)
{
// 得到查询表达式
MessageBox.Show(ucCombinQueryTest.QueryExpression);
}

7、下面给出组合查询控件完整代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值