BindingSource / DataSet / DataTable - Event when changed

  • Thread starter Thread starter Daniel Jeffrey
  • Start date Start date
D

Daniel Jeffrey

..Net 2.0
VStudio 2005
C#

I know this is going to seem like a strange question, as even I am sure I
have missed something - but I cant find it.

I want a simple event on any of the objects (BindingSource, DataSet,
DataTable) that fires when data in a bound control changes.

Eg someone types something, or they change the value in a pull down list etc
etc.

I want this so I can then fire the Enabled functions of my buttons (save
etc) so users only press them when needed.

Please let me I have missed something very simple. I have tried all the
events on the BindingSource but with no success.

Regards,

Daniel Jeffrey
 
Hi Daniel,

The underlying data fires this event. DataTable should fire
RowChanging/RowChanged. In case of business object you need to fire it
yourself by implementing the INotifyPropertyChanged interface and fire
PropertyChanged.

The code below demonstrates the use of INotifyPropertyChanged by having the
Enabled property bound to the CanSave property on the business object. For
this binding to work, TextValue needs to fire a PropertyChanged event if it
has changed or the button binding won't get updated.

protected override void OnLoad(EventArgs e)
{
TextBox textBox1 = new TextBox();
Controls.Add(textBox1);

Button button1 = new Button();
button1.Location = new Point(textBox1.Width, 0);
Controls.Add(button1);

BindingSource source = new BindingSource(new BusinessObject(),
"");

textBox1.DataBindings.Add("Text", source, "TextValue", false,
DataSourceUpdateMode.OnPropertyChanged);
button1.DataBindings.Add("Enabled", source, "CanSave");
}


public class BusinessObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

private string _textValue;

public string TextValue
{
get { return _textValue; }
set
{
_textValue = value;
PropertyChanged(this, new
PropertyChangedEventArgs("TextValue"));
}
}

public bool CanSave
{
get
{
if (string.IsNullOrEmpty(TextValue))
return false;
else
return TextValue.Length > 3;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top