如何使用propertymanager刷新winform控件值?

时间:2013-10-13 15:39:22

标签: c# winforms binding

我使用propertymanager将实体绑定到winform coltrols,但是当我将Entity设置为anther Entity时,coltrol的值不会更新。这是我的代码:

    private PropertyManager pm;
    txtItemId.DataBindings.Add("Value", Entity, "ItemId",true);
    txtItemName.DataBindings.Add("Text", Entity, "ItemName",true);
    pm=(PropertyManager)this.BindingContext[Entity];    
    void tsBtnQuery_Click(object sender, EventArgs e)
    {
        Table2QueryForm frm=new Table2QueryForm();          
        frm.ShowDialog();
        if(frm.Tag!=null)
        {
            Entity=(Table2)frm.Tag; 
        }
    }

1 个答案:

答案 0 :(得分:0)

一个简单的解决方案是使用BindingSource类,下面是您项目的示例。

以表格形式声明:

public BindingSource Source { get; set; }

在表单加载事件中:

private void FormLoad(object sender, EventArgs e)
{
    // I assume that Entity is a variable of type Table2, 
    // if not then change Table2 to any other class
    Source = new BindingSource(typeof(Table2), null);

    txtItemId.DataBindings.Add("Value", Source, "ItemId", true, DataSourceUpdateMode.OnPropertyChanged);
    txtItemName.DataBindings.Add("Text", Source, "ItemName", true, DataSourceUpdateMode.OnPropertyChanged);
}

处理按钮点击事件:

void tsBtnQuery_Click(object sender, EventArgs e)
{
    Source.DataSource = (Table2)frm.Tag;
}