简体   繁体   English

设置DataSource时从ListBox中删除项目

[英]Remove items from ListBox when DataSource is set

See I have a HashSet with several values, this values can contain for example numbers like 4141234567 , 4241234567 , 4261234567 and so on. 见我有一个HashSet的几个值,该值可以包含例如号码,如4141234567,4241234567,4261234567等。 I put a radioButton1 in my UserControl and I want when I click this just the numbers with 414 and 424 remains on my ListBox, for that I wrote this code: 我在我的UserControl中放了一个radioButton1,当我点击这个时,我想要414和424的数字保留在我的ListBox上,因为我编写了这段代码:

private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        var bdHashSet = new HashSet<string>(bd);

        if (openFileDialog1.FileName.ToString() != "")
        {
            foreach (var item in bdHashSet)
            {
                if (item.Substring(1, 3) != "414" || item.Substring(1, 3) != "424")
                {
                    listBox1.Items.Remove(item);
                }
            }
        }
    }

But when I run the code I get this error: 但是,当我运行代码时,我收到此错误:

Items collection cannot be modified when the DataSource property is set. 设置DataSource属性时,无法修改项集合。

What is the proper way to remove the non wanted items from the list without remove them from the HashSet? 从列表中删除不需要的项目而不从HashSet中删除它们的正确方法是什么? I'll add later a optionButton for numbers that begin with 0416 and 0426 and also a optionButton to fill the listBox with original values, any advice? 我稍后会添加一个optionButton用于以0416和0426开头的数字,还有一个optionButton来填充listBox的原始值,任何建议?

try 尝试

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    var bdHashSet = new HashSet<string>(bd);

    listBox1.Datasource = null;
    listBox1.Datasource =  bdHashSet.Where(s => (s.StartsWith("414") || s.StartsWith("424"))).ToList();
}

Try this: 尝试这个:

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    var bdHashSet = new HashSet<string>(bd);
    listBox1.Datasource = bdHashSet.Select(s => (s.Substring(1, 3) == "414" || s.Substring(1, 3) == "424"));

    //After setting the data source, you need to bind the data
    listBox1.DataBind();
}

I think that you can select the elements with linq and then reassign the listBox with the result. 我认为您可以使用linq选择元素,然后使用结果重新分配listBox。 In that way you dont need to remove elements from the list and you can keep the elements of the HashSet. 这样你就不需要从列表中删除元素,你可以保留HashSet的元素。

You can use BindingSource object. 您可以使用BindingSource对象。 Bind it with DataSource and then use the RemoveAt() method. 使用DataSource绑定它,然后使用RemoveAt()方法。

Try this : 尝试这个 :

DataRow dr = ((DataRowView)listBox1.SelectedItem).Row;
((DataTable)listBox1.DataSource).Rows.Remove(dr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C# 中为 ListBox 分配数据源时,如何从 ListBox 中删除所选项目? - How to remove selected items from ListBox when a DataSource is assigned to it in C#? 列表框错误:设置了DataSource属性时,无法修改项目集合 - listbox error: Items collection cannot be modified when the DataSource property is set 设置数据源时,从列表框中删除列表中的元素 - Deleting a Element in a List from a Listbox when the Datasource is Set 如何在不同的 colors 中为列表框中的项目着色? 出现异常:设置 DataSource 属性后无法修改 Items 集合 - How to color items in listBox in different colors? getting exception : Items collection cannot be modified when the DataSource property is set C#ListBox:设置DataSource属性时,无法修改Items集合 - C# ListBox : Items collection cannot be modified when the DataSource property is set 无法从列表框删除项目 - Cannot remove items from ListBox 如何使用C#中的字符串从设置了数据源的列表框中删除项目 - How to remove an item from listbox with datasource set, using string in C# 从列表框C#中删除项目时将剪切项目 - Items are cut when remove item from listbox c# 将 ListBox DataSource 属性设置为 null 以更改列表项是否错误? - Is it wrong to set ListBox DataSource property to null in order to change list Items? 更改选项卡时删除列表框项 - Remove listbox items when tab is changed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM