改变数据源时获得更好的方法来更新绑定控件 [英] Better way to update bound controls when changing the datasource

查看:162
本文介绍了改变数据源时获得更好的方法来更新绑定控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的问题。我有我的形式大量表单本身的文本框在一个属性中的数据绑定到田间地头。 。我改变不只是财产中的一个字段的值,但整个房地产(重置到以前保存的默认值)

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();



不行的,的BindingContext [行] PropertyManager中,而不是的CurrencyManager ,并没有刷新方法。 PushData()被保护,不可访问。我也打过电话 this.Refresh(),并单独对我试着打电话给每一个控制 c.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?

下面的IM目前使用的代码:

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, ""));



是窗体上的公共财产本身。 ResetRow 的形式,其分配到初始行的表格负荷和从未改变的克隆,它作为一个备份的私人财产。 的一些属性(但不是全部)的值允许为空,并且应显示为空字符串。这是NullableBinding类做幕后是什么

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?

推荐答案

这不是好多了,使用的 Binding.ReadValue 可能节省的解除绑定的麻烦和重绑定的假设你要绑定的对象保持不变的:

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();






如果,在另一方面,你正在改变底层绑定的对象,这是不行的。我建议把一个的BindingSource 绑定和之间的对象,这样一来,你只需要重新绑定绑定源,而不是所有50个单独绑定。无论是到 BindingSource.CancelEdit()或重置数据源的呼叫:


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;

这篇关于改变数据源时获得更好的方法来更新绑定控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆