Note that there are some explanatory texts on larger screens.

plurals
  1. POOdd problem with ListBox.DataSource
    primarykey
    data
    text
    <p>I'm writing a Windows application and using a Listbox control. I'm developing with Visual Studio C# 2008 Express Edition.</p> <p>I've got a data object that looks something like this</p> <pre><code>public class RootObject { public List&lt;SubObject&gt; MySubObjects{ get; set;} } </code></pre> <p>I've got a ListBox on my form, and also a property "MyRootObject" which, obviously, holds a RootObject. When the control is initialized, I set:</p> <pre><code>_listBox.DataSource = MyRootObject.MySubObjects; </code></pre> <p>Now, when the form loads, I debug and see that the DataSource is being set correctly. But nothing is displayed. I've overridden SubObject's ToString() method and it's not even being called. I tried setting _listBox.DisplayMember to a property of SubObject just to see if there was some problem there, but still nothing. I tried calling _listBox.Update() and _listBox.Refresh() after setting the DataSource, but still no love. The DataSource has all the data... it's just refusing to display it.</p> <p>So while debugging, I wondered WTF and I decided to just do </p> <pre><code>_listBox.DataSource = new List&lt;SubObject&gt;{ new SubObject(), new SubObject() }; </code></pre> <p>Sure enough, this worked, and I see two things listed in my listbox.</p> <p>So then, really curious, I decided to try copying the list of objects and putting that in the listbox, like so:</p> <pre><code>_listBox.DataSource = MyRootObject.MySubObjects.ToArray(); </code></pre> <p>This works! And it's a workaround to my problem for now... but a very annoying one. Does anyone know why I need to basically copy the list of objects like this to get it to work, rather than just setting the _listBox.DataSource = MyRootObject.MySubObjects; ? Again, the DataSource has all the right data either way after setting it... it's just when it's copied data, it actually displays, and when it's not, it's not displayed. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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