List Box DataBinding Issue

Hello,

I am having and issue and Im not quit sure why.  I am using VS2005 TS. I have created a Windows Forms Project.

On one of the forms, I have 2 listbox controls.

The first listbox control is bound to a custom object when the page is first loaded.  When  you select a value from this list, it populates the second listbox with data bound to another custom object.

This works fine.. however i have created a custom event that fires whenever the objects bound to the first listbox have changed.  The event DOES fire, however the listbox does not show the updated list (still has objects that were removed from the datasource)

I have stepped through the code and verified that the collection I am binding to is being updated, the update event fires, but the contents of the first listbox do not change...

For example:

void BindFirstList() // run at startup

BindFirstList(){
    listbox1.DataSource = myCustomObject();
}

CustomEvent(){
    // This definately fires!
    listbox1.DataSource = myCustomObject(); // the object IS different at this point, but the listbox1 still shows the data from when it was first bound.
}

Any suggestions are appreciated.


Answer this question

List Box DataBinding Issue

  • Renatas Lauzadis

    try calling the refresh method after your second binding!

    ListBox1.Refresh()



  • Brian Crawford

    thank you both for the answer!



  • Metrum

    ListBox doesn't update if its DataSource is reset to its previous value.  To work around this you can first set the listBox1.DataSource to null and then back to your list.

    Note that a better solution is for your list to provide its own change notification (be an IBindingList).  If you did this, then the listBox would automatically update rather than requiring you to manually update it based on changes.  The best way to create a list with change notification is to use our new generic BindingList - System.ComponentModel.BindingList<T>.

    Joe Stegman
    The Windows Forms Team
    Microsoft Corp.

    This posting is provided "AS IS" with no warranties, and confers no rights.


  • List Box DataBinding Issue