Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > listbox does not update when datasource changed
 

listbox does not update when datasource changed

I observed some behaviour I thought was oddon a listbox which is bound to a list (through setting the datasource property of the listbox). Initially the listbox is bound to list A and displays fine. I then want to sort the list so I create a new list (list B) and sort it, then I reset the listbox.DataSouce to the new list. Upon resetting the DataSource the listbox does not update, sometimes it just keeps displaying the old list, at othertimes the items disappear (except the scroll bar is still visible). I tracked this down to the fact I set the SelectionMode of the listbox to None, I don't know why this affects the resetting of the datasource, but if I surround my resetting code with .SelectionMode = SelectionMode.One and .SelectionMode = SelectionMode.None then the resetting works fine.

I don't know why this happens but if anyone else comes across this situation I hope this workaround can help out.

spanky4_3  Wednesday, September 24, 2008 12:45 AM

Hi spanky4_3,

I can reproduce your problems. When I set the the DataSource property of Listbox to null combined with setting listBox.SelectionMode= SelectionMode.None, the output window will display a "System.ArgumentException" exception. As far as I see, this exception may be caught by SetDataConnection(object newDataSource, BindingMemberInfo newDisplayMember, bool force). It didn’t block the application, but may cause the user interface not refresh.

From the above, we can see another workaround is calling the refresh method of ListBox after you set the DataSource property to false.

Hope it helps!

Best Regards,

Bruce Zhou

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Bruce.Zhou  Friday, September 26, 2008 6:25 AM

Use a BindingList instead of a list when you plan to databind to the data in the list. The bindinglist is setup for databinding

http://msdn.microsoft.com/en-us/library/ms132679.aspx

Ken Tucker  Wednesday, September 24, 2008 10:08 AM

Hi Ken, thanks for the reply. I played around with the listbox a bit more and found that the listbox does not display properly when; selectionmode is set to none, a list (or even bindinglist) is set as the datasource, the datasource is later set to nothing. If instead of setting the datasource to nothing I replace it with another list (or bindinglist) the itemsdisplay properly, however if I want to 'detach' the datasource by setting it to null/nothing the listbox will not display properly.

In the situation I described above I wanted to sort my list (by passing a delegate of Comparison(Of T)) which I can't seem to do with a bindinglist (unless I am missing something).

spanky4_3  Thursday, September 25, 2008 12:30 AM

Hi spanky4_3,

I can reproduce your problems. When I set the the DataSource property of Listbox to null combined with setting listBox.SelectionMode= SelectionMode.None, the output window will display a "System.ArgumentException" exception. As far as I see, this exception may be caught by SetDataConnection(object newDataSource, BindingMemberInfo newDisplayMember, bool force). It didn’t block the application, but may cause the user interface not refresh.

From the above, we can see another workaround is calling the refresh method of ListBox after you set the DataSource property to false.

Hope it helps!

Best Regards,

Bruce Zhou

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Bruce.Zhou  Friday, September 26, 2008 6:25 AM

Hi Bruce,

Thanks for your reply. I had tried call Refresh after I had reset the DataSource (which did not fix the problem) but I did not try Refresh after setting DataSource to null. I will try this and use this workaround.

Thanks,

Mark.

spanky4_3  Sunday, September 28, 2008 11:21 PM
Hi,
i have the same problem.
If i try to do this:

            lboClasses.DataSource = null;
            lboClasses.DataSource = classes;

The listbox isn't refreshed but if you click over an item the SelectedIndexChanged Event is raised correctely.

if i add these two lines:

            lboClasses.SelectionMode = SelectionMode.None;
            lboClasses.SelectionMode = SelectionMode.One;

All works fine, the listbox is refreshed and the items are selectable.

Another workaround is using this:

            ((CurrencyManager)lboClasses.BindingContext[lboClasses.DataSource]).Refresh();

In this way all works fine even without refresh or SelectionMode change

This bug is present only from version 2.0 of framework, in version 1.0 or 1.1 all works fine.

Thanks
Stefano

delfo  Monday, October 05, 2009 7:49 PM

You can use google to search for other answers

Custom Search

More Threads

• ComboBox/TextBox search functionallity
• DataGridViewComboBox need ID that corresponds to Displayed value
• ListBox and Hashtable
• Datagridview: Cell with more than one value from DB is readonly, what can i do?
• TableAdpater and Typed DataSet woes
• Auto generated Add.. methods and expression columns
• Adding Rows With a 2D Array
• DataGridView Performance questions
• how to disable navigation in child rows of a datagrid
• Progress bar and dataset