C#去除列表框项目 [英] C# removing items from listbox
本文介绍了C#去除列表框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表框从SQLDATA拉被填充,并将其拉了一些列的,我不希望像OBJECT_dfj,OBJECT_daskd。最关键的是所有这些与OBJECT_是,有没有办法从列表框中删除这些?我不能改变我的SQL语句
我想这:
的foreach(在listBox1.Items字符串项)
{
串removelistitem =对象;
如果(item.Contains(removelistitem))
{
listBox1.Items.Remove(项目);
}
}
但它给我的错误:
名单,这枚举势必已被修改。枚举器只能用来当列表不会改变。
块引用>
解决方案对不起球员,我有调整字符串
sqldatapull =博士[0]的ToString();
如果(sqldatapull.StartsWith(对象))
{
sqldatapull =;
}
listBox1.Items.Add(sqldatapull);
的for(int i = listBox1.Items.Count - 1; I> = 0;我 - )
{
如果(String.IsNullOrEmpty(listBox1.Items [I]为String ))
listBox1.Items.RemoveAt(ⅰ);
}
}
I have a listbox being populated from a SQLDATA pull, and it pulls down some columns that i dont want like OBJECT_dfj, OBJECT_daskd. The key is all of these being with OBJECT_, is there a way to remove these from the listbox? I cannot change my SQL statement.
i tried this:
foreach (string item in listBox1.Items) { string removelistitem = "OBJECT"; if(item.Contains(removelistitem)) { listBox1.Items.Remove(item); } }
but it gave me the error:
List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.
解决方案Sorry guys i had to adjust the string
sqldatapull = dr[0].ToString(); if (sqldatapull.StartsWith("OBJECT")) { sqldatapull = ""; } listBox1.Items.Add(sqldatapull); for (int i = listBox1.Items.Count - 1; i >= 0; i--) { if (String.IsNullOrEmpty(listBox1.Items[i] as String)) listBox1.Items.RemoveAt(i); } }
这篇关于C#去除列表框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文