Solved

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

Posted on 2004-04-05
6
1,230 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

896 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

13 Experts available now in Live!

Get 1:1 Help Now