Solved

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

Posted on 2004-04-05
6
1,259 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
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.

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FInd Image Control Gridview 3 35
abstract class C# 1 36
Amazon S3 Images with .Net 3 30
Updating a single record in forEach using C#/Entity 7 23
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 …
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

763 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