I have a quite complicated problem which I hope that I can explain right.
I have a very simple form with three text boxes, one binding source and one
binding navigator. The three text boxes are bound to the FirstName, LastName
and the Age property on the Employee class which you can see below.
Notice that I set the FirstName property in the LastName field.
1) If I change the LastName property in the bound textbox and leaves the
textbox the textbox bound to the firstname property is updated. This is ok.
2) If I change the LastName property in the bound textbox and press a
regular button the textbox bound to the firstname property is updated. Now
the focus has moved to the button, so this is actually the same scenario as
number 1.
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show(String.Format("First: {0}, Last: {1}, Age: {2}",
employeeList[2].FirstName,
employeeList[2].LastName,
employeeList[2].Age));
}
3) If I change the LastName property in the bound textbox and press a
toolbar button. The old values are being shown in the MessageBox, this is of
course wrong. I have therefore added a call the The EndEdit method on the
BindingSource. This gives me new problems. Now the MessageBox shows the
right values. But the textbox which is bound to the FirstName property shows
the old value. I would expect it to show the new value which I have set in
the LastName property. This is my problem!
I have also seen that when I make a call to EndEdit all textboxes are
commiting their values back to the bindingsource. Meaning, first is fx the
LastName property commiting its values back (and setting the value of the
FirstName property) then is the FirstName property commiting back overriding
the value which have been set by the LastName property. This is what happens
in my live project. The above is what happens in my test project. But I
guess that the error is very related.
private void toolStripButton1_Click(object sender, EventArgs e)
{
employeeBindingSource.EndEdit();
MessageBox.Show(String.Format("First: {0}, Last: {1}, Age: {2}",
employeeList[2].FirstName,
employeeList[2].LastName,
employeeList[2].Age));
}
Any ideas?
Best regards
Henrik Skak Pedersen.
Employee Class:
-------------------------
public class Employee : INotifyPropertyChanged
{
public Employee()
{
}
public Employee(string firstName, string lastName, int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
private string firstName;
public string FirstName
{
get { return firstName; }
set
{
if (value != this.firstName)
{
firstName = value;
NotifyPropertyChanged("FirstName");
}
}
}
private string lastName;
public string LastName
{
get { return lastName; }
set
{
if (value != this.lastName)
{
lastName = value;
NotifyPropertyChanged("LastName");
this.FirstName = "MY FIRSTNAME";
}
}
}
private int age;
public int Age
{
get { return age; }
set
{
if (value != this.age)
{
age = value;
NotifyPropertyChanged("Age");
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
}