DropDownList与ListBox控件的问题?

too1998 2010-05-11 12:04:20
例子:
select distinct typename from XC_WJMasterDevType where typename in('D-BET格式摄像机')
可以查询typename的某一个值。
现在我用一个DropDownList,把数据绑定在控件上,SQL语句
string sql="select distinct typename from XC_WJMasterDevType where typename in('"+this.DropDownList1.SelectedItem.Text.Trim() +"')"
每次只能找一个`~


现在我想用个ListBox控件~添加多个数据
用一个DropDownList控件,然后用一个btn_add_Click将DropDownList1里的内容添加到ListBox1里(可加多个)。
string sql="select distinct typename from XC_WJMasterDevType where typename in('"+this.ListBox1.SelectedItem.Text.Trim()+"')"

可是报错了~“未将对象引用设置到对象的实例”,请问一个如果用ListBox控件去实现多个选择功能的话,
代码要怎么实现?
...全文
179 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
too1998 2010-05-12
  • 打赏
  • 举报
回复
就是说`我加到LISTBOX1中的值~非要用鼠标点击才能加到string sql="select distinct typename from XC_WJMasterDevType where typename in('"+str+"')"
的where typename in('"+str+"')"中`~两三个记录的话非得按下CTRL进行多选取才能`选取中值~
我后来用这个:
string str="";
if(ListBox1.Items.Count>0)
{
foreach(ListItem item in ListBox1.Items)
{
this.sql+="select distinct typename from XC_WJMasterDevType where typename in('"+ str.Trim() +"','"+ item.Value +"','"+ item.Text +"')";
}
现在不用点击了`ADD进LISTBOX1中就可以了`
但是出现一个问题`比如ADD进`~两个记录 A和B~但上面的代码`~会这样走~(‘A’,‘B’,‘A’,‘B’)
怎么会这样啊`我要的结果是~(‘A’,‘B’)`
就是这个意思`~你的代码`是ADD记录进来了`~~但是生成报表时非要按下CTRL进行多选取才能`选取中值生成报表`而且是不点不行`不按不行`~
baoxuetianxia 2010-05-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 too1998 的回复:]
string str = "";
for (int i = 0; i < ListBox1.Items.Count; i++)//可以多选
{
if (ListBox1.Items[i].Selected)
{
str+=ListBox1.Items[i].Text+"‘,’";
}
}
str = str.Substring(0,str.Lengt……
[/Quote]
具体点 我不太明白啊
baoxuetianxia 2010-05-11
  • 打赏
  • 举报
回复

string sql="select distinct typename from XC_WJMasterDevType where typename in('"+this.ListBox1.SelectedItem.Text.Trim()+"')"

listBox1多选的话 this.ListBox1.SelectedItem.Text.Trim()这个只能读取第一个
改成如下的

string str = "";
for (int i = 0; i < ListBox1.Items.Count; i++)//可以多选
{
if (ListBox1.Items[i].Selected)
{
str+=ListBox1.Items[i].Text+",";
}
}
str = str.Substring(0,str.Length-1);
string sql="select distinct typename from XC_WJMasterDevType where typename in('"+str+"')"
daniel_duan 2010-05-11
  • 打赏
  • 举报
回复
先获取到typename
然后
this.ListBox1.Item.Add(new ListItem(typename ,typename ));
daniel_duan 2010-05-11
  • 打赏
  • 举报
回复
应该是string sql="select distinct typename from XC_WJMasterDevType where typename in('"+this.DropDownList1.SelectedItem.Text.Trim() +"')"

too1998 2010-05-11
  • 打赏
  • 举报
回复
string str = "";
for (int i = 0; i < ListBox1.Items.Count; i++)//可以多选
{
if (ListBox1.Items[i].Selected)
{
str+=ListBox1.Items[i].Text+"‘,’";
}
}
str = str.Substring(0,str.Length-1);
string sql="select distinct typename from XC_WJMasterDevType where typename in('"+str+"')"
这样就对了~唯一恶心地方就是加到LISTBOX中后,非要鼠标点击一个才算选取中?要按CTRL键才能多选取?
有没有办法BTN_ADD后就默认为选中啊?
Adechen 2010-05-11
  • 打赏
  • 举报
回复
循环选定的项,把text拼起来传入sql

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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