浮生 Э 2012-11-22
c# winform 多条件查找
20
我现在有2个textbox 一个是用户名,另一个是电话 现在想对这两个进行条件查找datagridview里的数据
string a = this.textBox2.Text.ToString().Trim();
string str = "select * from 表1 where 电话 like '%" + a + "%'";
SqlDataAdapter da = new SqlDataAdapter(str, conn);
da.Fill(ds, "表1");
dataGridView1.DataSource = ds.Tables["表1"].DefaultView;
string b = this.textBox1.Text.ToString().Trim();
string co = "select * from 表1 where 用户名 like '%" + a + "%'";
这是我的一段代码,已经能够对用户名进行查找,对电话进行查找的时候
string str = "select * from 表1 where 电话 like '%" + a + "%'"; 里str显示已经定义过 该怎么办
满意答案
流星紫愿 6级 2012-11-23
先把所有的结果 查询出来,不加条件放入到DataTable中,然后DataTable中有个Select方法,用这个,返回的是行数组,非常好用。
下面有个例子
dtShow是需要显示到DataGridView的表信息
DataTable dtShow;
dtPrison是所有的信息DataTable表,总信息表
rows 是 DataRow[] rows;
dttmp是临时的DataTable表
dtShow = dtPrison.Clone();
if (!string.IsNullOrEmpty(cbSheng1.Text))
{
rows = dtPrison.Select("省 = '" + cbSheng1.Text + "'");//省是否为空,相当于第一个文本框条件
foreach (DataRow thisrow in rows)
{
dtShow.Rows.Add(thisrow.ItemArray);
}
}
else
{
dtShow = dtPrison.Copy();
}
if (!string.IsNullOrEmpty(cbShi1.Text))//市是否为空,相当于你第二个文本框条件
{
dttmp = dtShow.Copy();
rows = dttmp.Select("市 = '" + cbShi1.Text + "'");
dtShow.Clear();
foreach (DataRow thisrow in rows)
{
dtShow.Rows.Add(thisrow.ItemArray);
}
}
if (!string.IsNullOrEmpty(cbQu1.Text))//区是否为空相当于第三个文本框条件
{
dttmp = dtShow.Copy();
rows = dttmp.Select("区 = '" + cbQu1.Text + "'");
dtShow.Clear();
foreach (DataRow thisrow in rows)
{
dtShow.Rows.Add(thisrow.ItemArray);
}
}
dataGridView.DataSource=dtShow;.//显示查询完毕后的结果
下面有个例子
dtShow是需要显示到DataGridView的表信息
DataTable dtShow;
dtPrison是所有的信息DataTable表,总信息表
rows 是 DataRow[] rows;
dttmp是临时的DataTable表
dtShow = dtPrison.Clone();
if (!string.IsNullOrEmpty(cbSheng1.Text))
{
rows = dtPrison.Select("省 = '" + cbSheng1.Text + "'");//省是否为空,相当于第一个文本框条件
foreach (DataRow thisrow in rows)
{
dtShow.Rows.Add(thisrow.ItemArray);
}
}
else
{
dtShow = dtPrison.Copy();
}
if (!string.IsNullOrEmpty(cbShi1.Text))//市是否为空,相当于你第二个文本框条件
{
dttmp = dtShow.Copy();
rows = dttmp.Select("市 = '" + cbShi1.Text + "'");
dtShow.Clear();
foreach (DataRow thisrow in rows)
{
dtShow.Rows.Add(thisrow.ItemArray);
}
}
if (!string.IsNullOrEmpty(cbQu1.Text))//区是否为空相当于第三个文本框条件
{
dttmp = dtShow.Copy();
rows = dttmp.Select("区 = '" + cbQu1.Text + "'");
dtShow.Clear();
foreach (DataRow thisrow in rows)
{
dtShow.Rows.Add(thisrow.ItemArray);
}
}
dataGridView.DataSource=dtShow;.//显示查询完毕后的结果
其他回答(2)
林小白 3级 2012-11-22
String sql = select * from T_Paiban ;
String condition="";
if(textBox1.Text.Trim()!="")
condition=" Num like '%" + textBox1.Text + "%' ";
if( textBox2.Text.Trim()!=""){
if(condition.Length>0){
condition+=“ or ”;
}
condition+="Name like '%" + textBox2.Text + "%'"
}
if( textBox3.Text.Trim()!=""){
if(condition.Length>0){
condition+=“ or ”;
}
condition+="TheWeek like '%" + textBox3.Text + "%'"
}
if(condition!="")
sql+=" where "+condition;应该就是这样的,如果还有编程方面的疑问,都是可以到问问堂找专业的编程方面的专家为你在线咨询作答的。
String condition="";
if(textBox1.Text.Trim()!="")
condition=" Num like '%" + textBox1.Text + "%' ";
if( textBox2.Text.Trim()!=""){
if(condition.Length>0){
condition+=“ or ”;
}
condition+="Name like '%" + textBox2.Text + "%'"
}
if( textBox3.Text.Trim()!=""){
if(condition.Length>0){
condition+=“ or ”;
}
condition+="TheWeek like '%" + textBox3.Text + "%'"
}
if(condition!="")
sql+=" where "+condition;应该就是这样的,如果还有编程方面的疑问,都是可以到问问堂找专业的编程方面的专家为你在线咨询作答的。