简体   繁体   中英

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. Setting the DataSource to Null allows it to be cleared with 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."

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.

Source: 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM