多个cheakbox组合的各种查询方法有没有简洁的代码

kiddkyd 2009-03-09 01:55:26

假如有2个cheakbox,就要写3种查询语句
假如有3个cheakbox,就要写6种查询语句
有没什么方法可以写得简便点的呢?

目的是:利用用户选的cheakbox的选项,把查询的结果显示在dataGridView上
...全文
93 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mawering 2009-03-09
  • 打赏
  • 举报
回复
帮顶了,学习一下!
北京的雾霾天 2009-03-09
  • 打赏
  • 举报
回复

string sql="...";
if (checkBox1.checked)
{
sql+=" and 条件1="...;
}
if (checkBox2.checked)
{
sql+=" and 条件2="....;
}
...
zgke 2009-03-09
  • 打赏
  • 举报
回复
你可以做循环..
private void button1_Click(object sender, EventArgs e)
{

IList<string> _CheckList = GetCheckBoxCheck(this);

//this窗体..你可以设置成容器 入Panel

}

public static IList<string> GetCheckBoxCheck(Control p_Control)
{
IList<string> _ReturnCheckText = new List<string>();
for (int i = 0; i != p_Control.Controls.Count; i++)
{
if (p_Control.Controls[i] is CheckBox)
{
CheckBox _Check = (CheckBox)p_Control[i];
if (_Check.Checked) _ReturnCheckText.Add(_Check.Text);
}
}
return _ReturnCheckText;
}

the_pain 2009-03-09
  • 打赏
  • 举报
回复

定义一个全局变量
public static string lcSql;
page_load赋值
Default2.lcSql = "select * from record where 1=1";
checkbox拼接

//CheckBoxList1
//时间周期
if ((this.txtbegintime.Text.Trim().Length != 0 && this.txtendtime.Text.Trim().Length != 0) && (this.CheckBoxList1.Items[0].Selected))
Default2.lcSql += "and accepttime between '" + this.txtbegintime.Text + "' and '" + this.txtendtime.Text + "' ";

//发送内容
if ((this.txtcontent.Text.Trim().Length != 0) && (this.CheckBoxList1.Items[1].Selected))
Default2.lcSql += " and content like'" + this.txtcontent.Text.Trim() + "%'";

//场所名称position
if ((this.txtplacename.Text.Trim().Length != 0) && (this.CheckBoxList1.Items[2].Selected))
Default2.lcSql += " and position like'" + this.txtplacename.Text.Trim() + "%'";


贫僧又回来了 2009-03-09
  • 打赏
  • 举报
回复
换成checklistbox会不会好点呢?

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧