Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-04-05
6
Medium Priority
?
1,297 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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 750 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

722 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