Solved

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

Posted on 2009-07-14
2
3,833 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
[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
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I bind a WPF ComboBox to an ItemSource using XAML? 2 32
MYSQL responding very slow 3 50
c# ftp code 3 33
Set arraylist as datatable 2 10
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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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