Note that there are some explanatory texts on larger screens.

plurals
  1. POListBox throwing ArgumentOutOfRangeException when adding to DataSource
    primarykey
    data
    text
    <p>I'm trying use a <code>BindingList</code> as a <code>DataSource</code> for a <code>ListBox</code> in C# WinForms, but whenever I try to add items to the <code>BindingList</code>, I get an <code>ArgumentOutOfRangeException</code> thrown. The following code demonstrates the problem (assume a form with <code>ListBox listBox1</code>):</p> <pre><code>BindingList&lt;string&gt; dataSource = new BindingList&lt;string&gt;(); listBox1.DataSource = dataSource; dataSource.Add("Test1"); // Exception, here. </code></pre> <p>Note that if <code>dataSource</code> already has items in it, I do not get the exception:</p> <pre><code>BindingList&lt;string&gt; dataSource = new BindingList&lt;string&gt;(); dataSource.Add("Test1"); listBox1.DataSource = dataSource; dataSource.Add("Test2"); // Appears to work correctly. </code></pre> <p>I can work around the problem by setting the <code>DataSource</code> property to <code>null</code> before adding an item, and re-setting the <code>DataSource</code> afterward, but this feels like a hack, and I'd like to be able to avoid doing so.</p> <p>Is there a (non-hack) way to use an empty <code>DataSource</code> on a <code>ListBox</code>, such that adding items to it doesn't throw exceptions?</p> <p><strong>Edit</strong>: Stack Trace:</p> <blockquote> <p>System.Windows.Forms.dll!System.Windows.Forms.ListBox.SelectedIndex.set(int value) + 0x1ec bytes<br> System.Windows.Forms.dll!System.Windows.Forms.ListControl.DataManager_PositionChanged(object sender, System.EventArgs e) + 0x2e bytes<br> System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.OnPositionChanged(System.EventArgs e) + 0x39 bytes<br> System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.ChangeRecordState(int newPosition, bool validating, bool endCurrentEdit, bool firePositionChange, bool pullData) + 0x14f bytes<br> System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.List_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e) + 0x2e4 bytes<br> System.dll!System.ComponentModel.BindingList.OnListChanged(System.ComponentModel.ListChangedEventArgs e) + 0x17 bytes<br> System.dll!System.ComponentModel.BindingList.FireListChanged(System.ComponentModel.ListChangedType type, int index) + 0x35 bytes<br> System.dll!System.ComponentModel.BindingList.InsertItem(int index, System._<em>Canon item) + 0x3f bytes<br> mscorlib.dll!System.Collections.ObjectModel.Collection.Add(System.</em>_Canon item) + 0x76 bytes</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload