Listbox / Combobox – Data binding using an IList variable

What a pain in the ass this was.  There are fifty thousand tutorials on the net talking about this and all of them give some variation of this:-

System.Collections.Arraylist list = new System.Collections.Arraylist();
list.add(new item(“Woot”));
list.add(new item(“Funky”));
list.add(new item(“EarthCrack”));

_randomListbox.DataSource = list;
_randomListbox.DisplayValue = “SmackTalk”;

Where item is a simple data structure with a string value called “SmackTalk” which will be used as the display string. This is where they all end!  Assuming that what, we’ve completely finished data processing and there will be no changes? No user input or interaction? Argh, I was tearing my hair out!  What they never mention is this.

Unless an IList also implements IBinding (which the most common IList, ArrayList, and its strongly typed sibling List<> don’t.), then changes to the list will NOT reflect as changes in the listbox.

The answer to this is so stupid its almost funny, except for the sheer amount of time (easily upwards of an hour) I spent on this problem tonight. Not because there arn’t other ways to do this (hell, give me a foreach loop any day), but because I hate it when I know something should be working and isn’t.

The solution?  Add this snippet underneath any changes you make to your IList and you want the listbox updated.

list.add(new item(“Killroy2.0IsEverywhere”));
BindingManagerBase bmb = _randomListBox.BindingContext[list];
bmb.SuspendBinding();
bmb.ResumeBinding();

Apparently suspending and resuming the binding process (something you normally do to save processing power and prevent constant refreshing during long updates I guess) forces the list to recheck the IList manually.  Thus, it updates for us.

Thank god for that at long last.

Advertisements

8 thoughts on “Listbox / Combobox – Data binding using an IList variable

  1. Fantastic, saved me hours.

    And in VB.Net

    List.Add(strNew)
    Dim bmb As BindingManagerBase = combo.BindingContext(List)
    bmb.SuspendBinding()
    bmb.ResumeBinding()

  2. There is another interface that can be used as datasource for a listbox, combobox, datagrid or checkedlistbox that implements IBinding. This interface is IBindingList and the generic class BindingList implement such interface.

    The implementation should look like this:

    // Code Snippet
    System.ComponentModel.BindingList list = new System.ComponentModel.BindingList;

    list.add(new item(”Woot”));
    list.add(new item(”Funky”));
    list.add(new item(”EarthCrack”));
    // End of code snippet

    Nothing else must be done, the bound control will be updated automatically when any change is done to the list or list elements.

  3. Where item is a simple data structure with a string value called “SmackTalk” which will be used as the display string. This is where they all end! Assuming that what, we’ve completely finished data processing and there will be no changes? No user input or interaction? Argh, I was tearing my hair out! What they never mention is this.

    Unless an IList also implements IBinding (which the most common IList, ArrayList, and its strongly typed sibling List don’t.), then changes to the list will NOT reflect as changes in the listbox.

    The answer to this is so stupid its almost funny, except for the sheer amount of time (easily upwards of an hour) I spent on this problem tonight. Not because there arn’t other ways to do this (hell, give me a foreach loop any day), but because I hate it when I know something should be working and isn’t.

  4. Where item is a simple data structure with a string value called “SmackTalk” which will be used as the display string. This is where they all end! Assuming thatinput or interaction? Argh, I was tearing my hair out! What they never mention is this.

    Unless an IList also implements IBinding (which the most common IList, ArrayList, and its strongly typed sibling List don’t.), then changes to the list will NOT reflect as changes in the listbox.

    The answer to this is so stupid its almost funny, except for the sheer amount of time (easily upwards of an hour) I spent on this problem tonight. Not because there arn’t other ways to do this (hell, give me a foreach loop any day), but because I hate it when I know something should be working and isn’t.

  5. Dude. Great write up. I’ve not written an WinForms apps in a while. Found myself cursing like a sailor at this stupid issue since seven AM.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s