Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-07-14
2
Medium Priority
?
3,998 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

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

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…
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.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline

783 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