Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Databinding objects - Two-way binding problem?!
 

Databinding objects - Two-way binding problem?!

Hello,

I have a problem regrading data binding objects. I wrote a simple form that handles the management of users. I databound the textbox by code as follows:

Code Snippet

txtUserName.DataBindings.Add("Text", oUser, "Username");

No problem here, if I change the value in my textbox the property gets changed in the object. The problem I have is with the code below:

Code Snippet

oUser.Username = "John Doe";

oUser.Password = "xxx";

oUser.Level = "user";

When I changed the properties in the code,they get updated in the object but the textbox doesn't show the changes. Can anyone help me out on this issue?

Thanks in advance!

PS: The username property is as follows:

Code Snippet

public string Username

{

get { return _username; }

set {

try

{

if (usernameChanged != null)

{

if (value == "")

{

usernameChanged(this, new ErrorEventArgs("Username" , "The username is required. Please enter a value"));

_username = "";

}

else

{

usernameChanged(this, new ErrorEventArgs("Username", ""));

_username = value;

}

}

else

{

Console.WriteLine("Eventhandeler is not called for");

}

}

catch (Exception ex)

{

throw(ex);

}

}

}

Dries Robeyns  Wednesday, June 20, 2007 3:16 PM

The databinding mechanism built in the .NET Framework looks for one of two events:

1. A PropertyChanged event from a class that implements INotifyPropertyChanged, or

2. For a property named XyZ, an XyZChanged event where case is important.

Since your event was called usernameChanged, it did not update when property Username was changed. Either change your property name or the name of the event.

Tony

tkerns  Thursday, June 21, 2007 2:40 AM

Ok for some unknown reason it does seem to work when I write my propertyname as username instead of Username...

Anyone have any idea why?

Dries Robeyns  Wednesday, June 20, 2007 6:48 PM

The databinding mechanism built in the .NET Framework looks for one of two events:

1. A PropertyChanged event from a class that implements INotifyPropertyChanged, or

2. For a property named XyZ, an XyZChanged event where case is important.

Since your event was called usernameChanged, it did not update when property Username was changed. Either change your property name or the name of the event.

Tony

tkerns  Thursday, June 21, 2007 2:40 AM
Thank you very much! This cleared up this part.
Now I am stuck at the problem that my databindings with custom events dont work in usercontrols for some reason.

The full story here:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1762274&SiteID=1
Dries Robeyns  Thursday, June 21, 2007 2:24 PM

You can use google to search for other answers

Custom Search

More Threads

• DoDrapDrop Kills SelectedIndexChanged Event Handler?
• DataGridView eating keystrokes?
• datagrid - read data from one location (non standard connection) and update SQL
• flat combo box
• Typed DataSet and Relationship Name
• Is there a bug w/ the DataGridView?
• Datagridview comboboxcolumn selection should automatic set values in other columns.
• DefaultValuesNeedeed values ignored
• Prevent insert in a datagrid
• Unable to catch MouseDown event of ListView in .NET CF