Nevyn
Nevyn

Reputation: 2683

Better way to update bound controls when changing the datasource

The problem. I have a databinding to the fields within a property on my form for a large number of textboxes on the form itself. I am changing not just a field value within the property, but the entire property (resetting to a previously saved default).

What I wanted to do was something like this:

((CurrencyManager)BindingContext[Row]).Refresh();

Doesn't work, BindingContext[Row] is a PropertyManager, not a CurrencyManager, and doesn't have a refresh method. PushData() is protected and not accessable. I also tried calling this.Refresh(), and individually on each control I tried calling c.Refresh...Now What?

Here's the code Im currently using:

private void btnReset_ButtonClicked(object sender, EventArgs e)
{
    Row = ResetRow.Clone();//use clone to break the reference, dont want to be updating the ResetRow when Row fields change.

    foreach (Control c in pnlBody.Controls)
        if (c.DataBindings.Count > 0)
            c.DataBindings.Clear();

    DataBindandSet();//re-apply all the databindings (50)
}

//just a sample of the databinding function itself
private void DataBindandSet()
{
    txtAzimuth.DataBindings.Add(new NullableBinding("Text", Row, "Azimuth", true));
    txtBHTemp.DataBindings.Add(new NullableBinding("Text", Row, "BHTemp", true));
    //repeat 48 more times
}

Update I set the databindings to the full databinding string, to see if setting the null value default had any effect, it didnt.

txtAzimuth.DataBindings.Add(new NullableBinding("Text", Row, "Azimuth", true, DataSourceUpdateMode.OnPropertyChanged, ""));

Row is a public property on the form itself. ResetRow is a private property on the form, its assigned to a clone of the initial row on Form Load and never changed, it serves as a backup. The value of some of the properties (but not all) is allowed to be null, and should display as an empty string. This is what the NullableBinding class does behind the scenes

Can anyone think of a better way to do this than removing and re-applying every single one of the databindings?

Upvotes: 4

Views: 14099

Answers (2)

TCool
TCool

Reputation: 1

Try this one: TableAdapter.Fill(dataTable)

My example: this.bankingaccountTableAdapter.Fill(this.bankDataSet.bankingaccount);

Upvotes: 0

lc.
lc.

Reputation: 116538

It's not a whole lot better, using Binding.ReadValue might save the hassle of unbinding and rebinding, assuming the object you are binding to remains the same:

foreach (Control c in pnlBody.Controls)
    foreach (Binding b in c.DataBindings)
        b.ReadValue();

If, on the other hand, you are changing the underlying bound object, this will not work. I suggest putting a BindingSource between the Bindings and the Row object, this way you only have to re-bind the binding source and not all 50 bindings individually. Either by a call to BindingSource.CancelEdit() or by resetting the data source:

RowBindingSource.DataSource = null;
RowBindingSource.DataSource = this.Row;

Upvotes: 7

Related Questions