添加到 DataSource 时 ListBox 抛出 ArgumentOutOfRangeException
我尝试在 C# WinForms 中使用 BindingList
作为 ListBox
的 DataSource
,但是每当我尝试将项目添加到 BindingList
,我抛出一个 ArgumentOutOfRangeException
。以下代码演示了该问题(假设带有 ListBox listBox1
的表单):
BindingList<string> dataSource = new BindingList<string>();
listBox1.DataSource = dataSource;
dataSource.Add("Test1"); // Exception, here.
请注意,如果 dataSource
中已包含项目,则我不会收到异常:
BindingList<string> dataSource = new BindingList<string>();
dataSource.Add("Test1");
listBox1.DataSource = dataSource;
dataSource.Add("Test2"); // Appears to work correctly.
我可以解决此 问题通过在添加项目之前将 DataSource
属性设置为 null
来解决问题,然后重新设置 DataSource
,但这感觉像是一个黑客,我希望能够避免这样做。
是否有一种(非黑客)方法可以在 ListBox
上使用空的 DataSource
,这样向其中添加项目就不会引发异常?
编辑:堆栈跟踪:
System.Windows.Forms.dll!System.Windows.Forms.ListBox.SelectedIndex.set(int 值) + 0x1ec 字节
System.Windows.Forms.dll!System.Windows.Forms.ListControl.DataManager_PositionChanged(对象 发送者,System.EventArgs e) + 0x2e 字节
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.OnPositionChanged(System.EventArgs e) + 0x39 字节
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.ChangeRecordState(int newPosition,布尔验证,布尔 结束当前编辑,布尔 firePositionChange, bool pullData) + 0x14f字节
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.List_ListChanged(对象 发件人, System.ComponentModel.ListChangedEventArgs e) + 0x2e4 字节
System.dll!System.ComponentModel.BindingList.OnListChanged(System.ComponentModel.ListChangedEventArgs e) + 0x17 字节
System.dll!System.ComponentModel.BindingList.FireListChanged(System.ComponentModel.ListChangedType 类型,int 索引)+ 0x35 字节
System.dll!System.ComponentModel.BindingList.InsertItem(int 索引,System._佳能项目) + 0x3f 字节
mscorlib.dll!System.Collections.ObjectModel.Collection.Add(System._Canon 项目) + 0x76 字节
I'm trying use a BindingList
as a DataSource
for a ListBox
in C# WinForms, but whenever I try to add items to the BindingList
, I get an ArgumentOutOfRangeException
thrown. The following code demonstrates the problem (assume a form with ListBox listBox1
):
BindingList<string> dataSource = new BindingList<string>();
listBox1.DataSource = dataSource;
dataSource.Add("Test1"); // Exception, here.
Note that if dataSource
already has items in it, I do not get the exception:
BindingList<string> dataSource = new BindingList<string>();
dataSource.Add("Test1");
listBox1.DataSource = dataSource;
dataSource.Add("Test2"); // Appears to work correctly.
I can work around the problem by setting the DataSource
property to null
before adding an item, and re-setting the DataSource
afterward, but this feels like a hack, and I'd like to be able to avoid doing so.
Is there a (non-hack) way to use an empty DataSource
on a ListBox
, such that adding items to it doesn't throw exceptions?
Edit: Stack Trace:
System.Windows.Forms.dll!System.Windows.Forms.ListBox.SelectedIndex.set(int
value) + 0x1ec bytes
System.Windows.Forms.dll!System.Windows.Forms.ListControl.DataManager_PositionChanged(object
sender, System.EventArgs e) + 0x2e
bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.OnPositionChanged(System.EventArgs
e) + 0x39 bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.ChangeRecordState(int
newPosition, bool validating, bool
endCurrentEdit, bool
firePositionChange, bool pullData) +
0x14f bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.List_ListChanged(object
sender,
System.ComponentModel.ListChangedEventArgs
e) + 0x2e4 bytes
System.dll!System.ComponentModel.BindingList.OnListChanged(System.ComponentModel.ListChangedEventArgs
e) + 0x17 bytes
System.dll!System.ComponentModel.BindingList.FireListChanged(System.ComponentModel.ListChangedType
type, int index) + 0x35 bytes
System.dll!System.ComponentModel.BindingList.InsertItem(int
index, System._Canon item) + 0x3f
bytes
mscorlib.dll!System.Collections.ObjectModel.Collection.Add(System._Canon
item) + 0x76 bytes
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实证明,我在“异常”对话框中检查了所有内容(调试->异常)。因此,异常确实存在,但由 .Net 框架(默默地)处理。继续执行程序会显示预期的结果。
It turns out I had everything checked in the "Exceptions" dialog (Debug->Exceptions). So, the exception exists, but is (silently) handled by the .Net framework. Continuing program execution displays the expected results.
您是否可能在
ListBox
上附加了一个可能导致此问题的事件处理程序?我无法重现您所描述的行为。我创建了一个完全空白的 WinForms 项目,其中有一个
ListBox
绑定到BindingList
,将值“Test”添加到列表中(在设置ListBox.DataSource
属性),并且项目“Test”出现在框中,如预期的那样。我会查看您的
ListBox
以及BindingList
,看看其中任何一个是否具有您可能缺少的一些附加事件处理程序。Do you possibly have an event handler attached to some event on your
ListBox
that could be causing this? I am not able to reproduce the behavior you're describing.I created a completely blank WinForms project, with a single
ListBox
bound to aBindingList<string>
, added the value "Test" to the list (after setting theListBox.DataSource
property), and the item "Test" appeared in the box, as expected.I'd take a look at your
ListBox
as well as yourBindingList<string>
to see if either one has some attached event handlers you might be missing.我遇到了同样的问题,经过多次研究,我发现避免此 .Net 错误的唯一解决方法是当列表不为空时仅将 BindingList 分配给 DataSource。
如果它可以更改,您可以创建一个始终保留在列表中的虚拟对象,并在列表不为空时将其删除。
最后,寻找一种方法来避免抛出 ArgumentOutOfRangeException 是不值得的。
I had the same problem and after several research, I found that the only workaround to avoid this .Net error was to assign only the BindingList to the DataSource when the list is not empty.
If it can change, you can make a dummy object that you always keep in the list, and you removed it when the list is not empty.
Finaly, its not worth it to find a way to avoid ArgumentOutOfRangeException to be thrown.