[英]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:如果您按此顺序单击按钮,则一切正常:
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.