Solved

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

Posted on 2004-04-05
6
1,269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

738 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