Solved

Fire one event when a BindingSource's Current object's property changes.

Posted on 2009-07-14
2
3,809 Views
Last Modified: 2013-12-17
I have a BindingSource (employeeBindingSource) who's DataSource is an object array (Employee[]).

It is bound to a BindingNavigator, a ToolStripComboBox, and several Common Windows Form Controls.

GOAL: Fire only one event when the user changes a bound value. Such as text in a TextBox, state of a CheckBox, value of a DateTimePicker, etc.

PROBLEM: I am handling the CurrentItemChanged event but whenever the user changes a text value, this event gets fired twice on validation. According to MSDN, this is expected since it is fired once for the property change and again for the CurrentChanged.

I tried to handle the ListChanged event and checked the ListChangedType for ItemChanged. I got the same results. The event was fired twice!

QUESTION:
So, which event(s) will allow me to detect changes in a BindingSource's Current properties just once per change?
employeeBindingSource.DataSource = _hrClient.GetEmployees(null, null, true, false);
employeeBindingSource.CurrentItemChanged += employeeBindingSource_CurrentItemChanged;
employeeBindingSource.ListChanged += employeeBindingSource_ListChanged;
bindingNavigator.BindingSource = employeeBindingSource;
 
......
 
// The following methods allowed me to see the events fired multiple times
 
private void employeeBindingSource_CurrentItemChanged(object sender, EventArgs e)
{
    string text = lstPastStudiesEmp.Items.Count + " CurrentItemChanged";
    listbox1.Items.Insert(0, text);
}
 
private void employeeBindingSource_ListChanged(object sender, ListChangedEventArgs e)
{
    string text = lstPastStudiesEmp.Items.Count + " " + e.ListChangedType;
    lstPastStudiesEmp.Items.Insert(0, text);
}

Open in new window

0
Comment
Question by:RyanAndres
2 Comments
 
LVL 12

Expert Comment

by:topdog770
ID: 24864971
Have you tried just using CurrentChanged ?  It should fire anytime the data changes, but not until then.
 
 
 
0
 
LVL 8

Accepted Solution

by:
RyanAndres earned 0 total points
ID: 24865206
CurrentChanged will fire everytime the Current changes (the position) rather than its properties.

I have found a workaround, which is to use the PropertyChanged event on my DataSource's objects except this isn't built into the BindingSource itself. Before I assign my dataobjects to the datasource I attach each object's PropertyChanged event to an event handler for further processing whenever a value changes.

I will use this solution for now until I find a better one that works specifically with bindingsources.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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