Groups | Blog | Home
all groups > dotnet windows forms databinding > july 2007 >

dotnet windows forms databinding : Problem with BindingSource.EndEdit


Henrik Skak Pedersen
7/6/2007 1:17:03 PM
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
}

v-lliu NO[at]SPAM online.microsoft.com
7/9/2007 3:37:35 AM
Hi Henrik,

I performed a test based on your description and saw the same thing as you
did.

In your scenario, when you change the LastName property in the bound
TextBox and leaves the TextBox or clicks on a Button, the pending changes
are applied to the underlying data source automatically.

However, if you click on a ToolStripButton, the focus isn't taken away from
the TextBox, so the data binding won't apply the pending changes to the
underlying data source.

In this case, as you have mentioned, we could call the BindingSource.
EndEdit method to force the pending changes to be applied to the data
source. After calling the EndEdit method, you'll see the new values are
committed to the data source, but the bound TextBoxes still show the old
values. This is a data binding's refresh problem. We could call the related
CurrencyManager's Refresh method to force a repopulation of data-bound list.

The following is a sample.

private void toolStripButton1_Click(object sender, EventArgs e)
{
this.employeeBindingSource.EndEdit();
this.employeeBindingSource.CurrencyManager.Refresh();
}

Please try my suggestion and let me know the result.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
v-lliu NO[at]SPAM online.microsoft.com
7/11/2007 11:14:04 AM
Hi Henrik,

How about the problem now?

If the problem is still not solved, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
AddThis Social Bookmark Button