简体   繁体   English

将 ListBox DataSource 属性设置为 null 以更改列表项是否错误?

[英]Is it wrong to set ListBox DataSource property to null in order to change list Items?

I found that Items.Clear does not always clear a listbox when the listbox has been filled via a DataSource.我发现当列表框已通过数据源填充时,Items.Clear 并不总是清除列表框。 Setting the DataSource to Null allows it to be cleared with Items.Clear().将 DataSource 设置为 Null 允许使用 Items.Clear() 清除它。

Is this the wrong way to do it this way?这是这样做的错误方法吗? Is my thinking a bit wrong to do this?这样做是不是我的想法有点错误?

Thanks.谢谢。

Below is the code I prepared to illustrate my problem.下面是我准备用来说明我的问题的代码。 It includes one Listbox and three buttons.它包括一个列表框和三个按钮。

If you click the buttons in this order everything Everything works:如果您按此顺序单击按钮,则一切正常:

  1. Fill List With Array button使用数组按钮填充列表
  2. Fill List Items With Array button使用数组按钮填充列表项
  3. Fill List Items With DataSource button使用数据源按钮填充列表项

But if you click the "Fill List Items With DataSource" button first, clicking on either of the other two buttons causes this error: "An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll" with "Items collection cannot be modified when the DataSource property is set."但是,如果您首先单击“使用数据源填充列表项”按钮,则单击其他两个按钮中的任何一个都会导致此错误:“System.Windows.Forms.dll 中发生类型为‘System.ArgumentException’的未处理异常”,并带有“设置 DataSource 属性后,无法修改项目集合。”

Comments?注释?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnFillListWithArray_Click(object sender, EventArgs e)
    {
       string[] myList = new string[4];

        myList[0] = "One";
        myList[1] = "Two";
        myList[2] = "Three";
        myList[3] = "Four";
        //listBox1.DataSource = null;  <= required to clear list
        listBox1.Items.Clear();
        listBox1.Items.AddRange(myList);
    }

    private void btnFillListItemsWithList_Click(object sender, EventArgs e)
    {
        List<string> LStrings = new List<string> { "Lorem", "ipsum", "dolor", "sit" };
        //listBox1.DataSource = null;  <= required to clear list
        listBox1.Items.Clear();            
        listBox1.Items.AddRange(LStrings.ToArray());

    }

    private void btnFillListItemsWithDataSource_Click(object sender, EventArgs e)
    {
        List<string> LWords = new List<string> { "Alpha", "Beta", "Gamma", "Delta" };
        //listBox1.DataSource = null;  <= required to clear list
        listBox1.Items.Clear();
        listBox1.DataSource = LWords;

    }
}

According to Microsoft it looks like setting the Datasource to Null then Clearing the list is acceptable.根据 Microsoft 的说法,似乎将数据源设置为 Null 然后清除列表是可以接受的。

Source: http://support.microsoft.com/kb/319927来源: http : //support.microsoft.com/kb/319927

If your listbox is bound to a datasource, then that datasource becomes the 'master' of the listbox.如果您的列表框绑定到一个数据源,那么该数据源将成为列表框的“主”。 You then don't clear the listbox, but you need to clear the datasource.然后您不清除列表框,但您需要清除数据源。 So if the listbox is bound to LWords, you do Lwords.clear() and the listbox would be cleared.因此,如果列表框绑定到 LWords,则执行 Lwords.clear() 并且列表框将被清除。 And that is correct behaviour, because that is what being databound is all about.这是正确的行为,因为这就是数据绑定的全部内容。

If you set the datasource to null, you are basically telling the listbox that it is no longer databound.如果您将数据源设置为 null,您基本上是在告诉列表框它不再是数据绑定的。 And of course as a side effect of that it becomes empty.当然,它的副作用是它变空了。 But depending on the situation you might not want the listbox just to be cleared, but you might want to clear the datasource and the listbox both.但是根据情况,您可能不希望只清除列表框,但您可能希望同时清除数据源和列表框。

Suppose you want to clear LWords via your GUI, and that LWords is the source of your listbox, you press a button and you set the datasource to null, you see the listbox becoming empty, thinking that LWords is not empty, but LWords is not empty at all, and then in this situation that would be a bug.假设您想通过 GUI 清除 LWords,并且 LWords 是您的列表框的来源,您按下一个按钮并将数据源设置为 null,您看到列表框变空,认为 LWords 不是空的,但 LWords 不是空,然后在这种情况下,这将是一个错误。

暂无
暂无

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

相关问题 列表框错误:设置了DataSource属性时,无法修改项目集合 - listbox error: Items collection cannot be modified when the DataSource property 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 列表框在数据源更改期间清除所有项目 - Listbox clears all items during datasource change 设置DataSource时从ListBox中删除项目 - Remove items from ListBox when DataSource is set C#ListBox:设置DataSource属性时,无法修改Items集合 - C# ListBox : Items collection cannot be modified when the DataSource property is set 选择列表中的项目时如何将属性值设置为空 - How to set a property value to null when select items in a list 组合框的BindingList数据源正确刷新,但组合框以错误的顺序显示项目 - The BindingList Datasource of a Combobox refreshes correctly but the Combobox displays items in the wrong order 设置数据源时,从列表框中删除列表中的元素 - Deleting a Element in a List from a Listbox when the Datasource is Set 如何从类的通用列表设置列表框的数据源 - How to set datasource of listbox from generic list of class 例外:设置DataSource属性时,无法修改项集合 - Exception : Items collection cannot be modified when the DataSource property is set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM