ArrayList Databinding

B

Brian Johnson

I have a Listbox which uses an ArrayList as its
datasource. This works great until the contents of the
ArrayList change; the Listbox is not updated as I
expected.

Upon further digging, I found that this is the default
behavior. Other data collections (datatables, etc.)
should do this, but not ArrayList.

Can anyone help show me how to force the update in the
ListBox when the ArrayList changes? I would prefer to
use an ArrayList in this particular case.

Thxs!
 
T

Tom

When you think about it, this behavior makes sense. If you want the Listbox
to update its contents when a DataSource changes, it has to have some way of
knowing when the change has occurred. That's handled by the IBindingList
interface, which raises the ListChanged event, which is implemented only on
the DataView and DataViewManager classes. Take a look it in the
documentation. Here's a quote from the "Data Binding a Windows Forms
ComboBox or ListBox Control" topic in the docs:

"If you are bound to a data source that does not implement the IBindingList
interface (such as an ArrayList object), the bound control's data will not
be updated when the data source is updated. For example, if you have a combo
box bound to an ArrayList object and data is added to the ArrayList, these
new items will not appear in the combo box. However, you can force the combo
box to be updated by calling the SuspendBinding and ResumeBinding methods on
the instance of the BindingContext class to which the control is bound."

Tom Clement
Apptero, Inc.
 
B

Brian Johnson

Thanks for the response. This agrees with the info I dug
up as well. Unfortunately, I'm not smart enough to
figure out the sentence "However, you can force the combo
box to be updated by calling the SuspendBinding and
ResumeBinding methods on the instance of the
BindingContext class to which the control is bound."

How do I get the instance of the BindingContext class to
which the control is bound?
 
M

Matt Garven

Along these lines, you may find it easier to write a class which wraps
ArrayList and implements IBindingList. Call through to the appropriate
methods in the wrapped ArrayList, plus add the ListChange functionality
yourself.
 
I

Ian Cooper

Unfortunately, I'm not smart enough to figure out the sentence "However, you can force the combo box to be updated by calling the SuspendBinding and ResumeBinding methods on the instance of the BindingContext class to which the control is bound."

How do I get the instance of the BindingContext class to which the control is bound?<<
You can get the binding context from the container control that contains your listbox. Container controls inherit the BindingContext of their parent If you have not set the data source for any other container control in your hierachy, as I am guessing you have not, then the BindingContext you are looking for is on the form.
The BindingContext collection on the container control is indexed by DataSource. Whenever you add a binding with a new DataSource, you add a new BindingContext to the collection.
So if your ArrayList was called MyArrayList and you bound your listbox to it with the line:
MyListBox.DataSource = MyArrayList
When you want to retrieve the BindingContext you need to ask for
BindingContext[MyArrayList]
This returns a BindingManagerBase object. BindingManagerBase is an abstract base class with two concrete implementations: PropertyManager - for single item data sources and CurrencyManager - for multiple item data sources (the currency refers to the position in the data source).
So for an array list you retrieve a CurrencyManager (just cast it) which you can call SuspendBinding and ResumeBinding on. Actually you might want to try Refresh which might work for you in this instance, can't remember.
HTH,
Ian Cooper
wwww.dnug.org.uk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top