C# comboBox下拉菜单取值做判断
在winform中做了comboBox1,comboBox2,button1,datagridview1,点击button1要求根据comboBox1,和comboBox...
在win form中做了comboBox1,comboBox2,button1,datagridview1,点击button1要求根据comboBox1,和comboBox2选择的值来确定datagridview的显示内容。但是comboBox1和comboBox2如果有一个不做选择的话,就只根据一个取值来确定datagridview的取值,我的部分代码如下:
string a, b;
a = comboBox1.SelectedValue.ToString();
b = comboBox2.SelectedValue.ToString();
string sql = "select* from content where logo=' “+a+” ', and subject=' “+b+” ';
sqlBase s = new sqlBase();
DataTable t = new DataTable();
t = s.ExeSQLdt(sql);
dataGridView1.DataSource = t;
dataGridView1.Columns[0].Width = 45;
dataGridView1.Columns[2].Width = 30;
dataGridView1.Columns[3].Width = 30;
dataGridView1.Columns[4].Width = 280;
以上代码执行的结果就是根据comboBox1和comboBox2的共同取值来确定从数据库中筛选的结果,如果有一个comboBox的下拉菜单不做选择,执行结果就会报错。怎么在上面加一个判断,比如只选择了comboBox1的值,comboBox2不选择,执行结果为
string sql = "select* from content where logo=' “+a+” ';
sqlBase s = new sqlBase();
DataTable t = new DataTable();
t = s.ExeSQLdt(sql);
dataGridView1.DataSource = t;
dataGridView1.Columns[0].Width = 45;
dataGridView1.Columns[2].Width = 30;
dataGridView1.Columns[3].Width = 30;
dataGridView1.Columns[4].Width = 280;
跪求结果,在线等待,谢谢 展开
string a, b;
a = comboBox1.SelectedValue.ToString();
b = comboBox2.SelectedValue.ToString();
string sql = "select* from content where logo=' “+a+” ', and subject=' “+b+” ';
sqlBase s = new sqlBase();
DataTable t = new DataTable();
t = s.ExeSQLdt(sql);
dataGridView1.DataSource = t;
dataGridView1.Columns[0].Width = 45;
dataGridView1.Columns[2].Width = 30;
dataGridView1.Columns[3].Width = 30;
dataGridView1.Columns[4].Width = 280;
以上代码执行的结果就是根据comboBox1和comboBox2的共同取值来确定从数据库中筛选的结果,如果有一个comboBox的下拉菜单不做选择,执行结果就会报错。怎么在上面加一个判断,比如只选择了comboBox1的值,comboBox2不选择,执行结果为
string sql = "select* from content where logo=' “+a+” ';
sqlBase s = new sqlBase();
DataTable t = new DataTable();
t = s.ExeSQLdt(sql);
dataGridView1.DataSource = t;
dataGridView1.Columns[0].Width = 45;
dataGridView1.Columns[2].Width = 30;
dataGridView1.Columns[3].Width = 30;
dataGridView1.Columns[4].Width = 280;
跪求结果,在线等待,谢谢 展开
3个回答
展开全部
string a, b;
a = comboBox1.SelectedValue.ToString();
b = comboBox2.SelectedValue.ToString();
string sql = "select* from content where logo=' “+(String.IsNullOrEmpty(a)?"%":a)+” ', and subject=' “+(String.IsNullOrEmpty(b)?"%":b)+” ';
sqlBase s = new sqlBase();
DataTable t = new DataTable();
t = s.ExeSQLdt(sql);
dataGridView1.DataSource = t;
dataGridView1.Columns[0].Width = 45;
dataGridView1.Columns[2].Width = 30;
dataGridView1.Columns[3].Width = 30;
dataGridView1.Columns[4].Width = 280;
a = comboBox1.SelectedValue.ToString();
b = comboBox2.SelectedValue.ToString();
string sql = "select* from content where logo=' “+(String.IsNullOrEmpty(a)?"%":a)+” ', and subject=' “+(String.IsNullOrEmpty(b)?"%":b)+” ';
sqlBase s = new sqlBase();
DataTable t = new DataTable();
t = s.ExeSQLdt(sql);
dataGridView1.DataSource = t;
dataGridView1.Columns[0].Width = 45;
dataGridView1.Columns[2].Width = 30;
dataGridView1.Columns[3].Width = 30;
dataGridView1.Columns[4].Width = 280;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
string sql = "select* from content where 1=1";
if (a!="")
{ sql += " and logo='“+a+” ' ";}
if (b!= "")
{ sql += " and subject='“+b+” ' ";}
...
if (a!="")
{ sql += " and logo='“+a+” ' ";}
if (b!= "")
{ sql += " and subject='“+b+” ' ";}
...
追问
if(comboBox1.selectedItem!=null) 这样判断,不要再声明变量a,b会简便一些,不过仍然非常感谢
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-01-09
展开全部
HAL9000PLUS 的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询