Solved

How to raise a DataSourceChanged event for listbox controls (VB.NET)

Posted on 2004-04-05
6
1,221 Views
Last Modified: 2007-12-19
Here is some code to illustrate my question:

1. Set DataSource for a listbox control to point to a collection of classes (it's an ArrayList that I filled with instances of objects containing data read from a DB):

Me.lbAssetClasses.DataSource = strategy.AssetClasses
Me.lbAssetClasses.DisplayMember = "Code"

2. Add an item to the collection:

strategy.AssetClasses.Add(lkp)

3. Set DataSource to Nothing and then re-set in order to trigger firing of a DataSourceChanged event:

Me.lbAssetClasses.DataSource = Nothing
Me.lbAssetClasses.DataSource = strategy.AssetClasses
Me.lbAssetClasses.DisplayMember = "Code"


If I don't set the DataSource to Nothing first in step 3 above, the DataSourceChanged event is not fired and hence the listbox is not updated. This is understandable since the collection the DataSource is pointing to has not changed, only the content of the collection has changed. So my first thought was to raise a DataSourceChanged event within my code, but I was not able to do it and that's why I resorted to setting the pointer to Nothing (which forces the generation of such
event and hence makes my code works as I want it to).

My questions are:

(a) Can I raise a DataSourceChanged event directly within my code? If so, how? And if not, why not?

(b) If the answer to Q1 is no, is there a cleaner way to achieve my goal other than setting the DataSource to Nothing first?

Best,
Stefano
0
Comment
Question by:sdamiano
  • 3
  • 3
6 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10759643
Are you only displaying one item in the list box?  Was there a reason to bind to an array list?  Can you bind directly to the data set?  

Basically, what are you trying to do here?

Bob
0
 

Author Comment

by:sdamiano
ID: 10760120
Hello Bob,

I need to display all items in the list box, not just one. The user can select and add one or more items from a list of choices (9 right now, but this will grow a little bit) and I need to show on the UI all of the selected items. This data will then be stored in the DB and once set-up it does not change that often (we refer to it as static data in our system). This is then used in other components of the system to classify and group data for various analysis and reports.

The VB code I have shown in my earlier posting interfaces with a business layer that in turn interfaces with the DB. The business layer does not return data sets, but rather objects or collections of objects. Each object contains other objects, and the relationships that exist among the data tables used to build such objects are one-to-many or many-to-many. In addition I want to eventually publish this data via a web browser (right now I only have a Windows UI). So I find it easier and cleaner not to work with data sets in the presentation layer, but only with typed objects or collections of objects. All of the logic used to map from the DB to the various objects is isolated in the bus layer and I'd like it to keep it this way.

Hope this is clear enough.

Best,
Stefano

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10760144
Are you using a multi-column list box?  If you are going to bind the list box to an array, then you will have to manually add new items to the data grid yourself.  How are you extracting data out of the DB into the array list?

Bob
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:sdamiano
ID: 10760255
No, it's not a multi-column list box. The DB layer performs a query and extracts the data into a data set. This is returned to the business layer, which does all the mapping to create the collection of objects to return to the presentation layer, which is then bound to a window control as necessary.

As I have shown in the first posting, I am setting the listbox data source property to point to the object collection, so I am not manually adding them to the listbox collection property. And this works, my original question was whether or not I can raise a DataSourceChanged event and avoid setting the DataSource to Nothing and then re-set it (which also works just fine, but I'd rather raise the event, if I can, it seems a cleaner way to do it). Do you have any comment on this specific point?

Thanks,
Stefano
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 10760373
Look here:

Data Binding a Windows Forms ComboBox or ListBox Control
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskdatabindingcomboboxcheckedlistboxorlistboxcontrol.asp

>>NOTE:   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.

     Try:

      Dim bindingMgr As BindingManagerBase = BindingContext( strategy.AssetClasses)

      bindingMgr.SuspendBinding()

      strategy.AssetClasses.Add(lkp)

      bindingMgr.ResumeBinding()

Bob
0
 

Author Comment

by:sdamiano
ID: 10760642
Ok, your pointer is a good one and the suggestion works. And I do understand much better now the binding of controls to ArrayLists.

Thanks for the help.

Best,
Stefano
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now