Reputation: 300
I bind my control to a dataset like this:
txt.DataBindings.Add("Text", ds, true, DataSourceUpdateMode.OnPropertyChanged);
Then I add listeners for changes like this
private void attatchChangeListeners(DataSet ds)
{
foreach (DataTable dt in ds.Tables)
{
dt.RowChanged += new DataRowChangeEventHandler(dt_RowChanged);
dt.RowDeleted += new DataRowChangeEventHandler(dt_RowDeleted);
dt.TableNewRow += new DataTableNewRowEventHandler(dt_TableNewRow);
}
}
void dt_TableNewRow(object sender, DataTableNewRowEventArgs e)
{
if (!View.Dirty)
{
View.Dirty = true;
}
}
void dt_RowDeleted(object sender, DataRowChangeEventArgs e)
{
if (!View.Dirty)
{
View.Dirty = true;
}
}
void dt_RowChanged(object sender, DataRowChangeEventArgs e)
{
if (!View.Dirty)
{
View.Dirty = true;
}
}
But when i change value in textbox in form i do not get the events.
I have to call endEdit on all rows for the events to be triggered.
Have i misunderstood the databinding or am i doing something else wrong?
Upvotes: 1
Views: 1823
Reputation: 2523
Yes at "misunderstood" :-).
Databinding can be thought as a middle layer between the UI objects and the DataSouce (typically a DataTable in a DataSet). To get the changes from UI take effect in the underlying DataSource, you have to call the BindingSource's EndEdit. Likewise, if you decide NOT to you can always cancel the changes using the "CancelEdit" (that is, if you want to revert the changes made by the UI to get back your DataSource's values). You might want to wire the CurrentItemChanged Event of the BindingSource to suit your requirement.
Upvotes: 2