如何在C#用户控制中刷新绑定到DataSource的控件 [英] How do I refresh Controls bound to a DataSource in C# User Control

查看:985
本文介绍了如何在C#用户控制中刷新绑定到DataSource的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个控件,让我有一个控件,让我称它为主机,我又有一个叫客户端。



主机包含多个客户端实例



我正在从主机控制台更新这些BindingSources,但是,客户端显示了当前标签中的四个主要变量,它们绑定到每个控件的BindingSource。使用LINQ检索的数据库值。



当我从主机控件更新BindingSource时,在客户端控件中不更新的值就像我预期的那样



任何人都可以快速说明我需要调用的任何方法,以便在BindingSource更改时确保标签刷新最新数据? p>

请问,

解决方案

说明



您必须实现 INotifyPropertyChanged 界面。


INotifyPropertyChanged接口用于通知客户端(通常是绑定客户端)属性值已更改。




样本



  public class MyClass:INotifyPropertyChanged 
{
public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info)
{
if(PropertyChanged!= null)
{
PropertyChanged(this,new PropertyChangedEventArgs(info) ;
}
}

私有字符串myProperty;
public string MyProperty
{
get
{
return this.myProperty;
}

set
{
if(value!= this.myProperty)
{
this.myProperty = value;
NotifyPropertyChanged(MyProperty);
}
}
}
}



更多信息




I am slightly confused in my understanding of DataSources in Winforms, and was hoping somebody might be able to clear something up for me.

I have one control, lets call it Host, and I have another one, called Client.

"Host" contains multiple instances of "Client"

Client displays four main variables, in labels at the moment, which are bound to a BindingSource per control.

I am updating these BindingSources from the Host control however, using database values retrieved using LINQ.

When I update the BindingSource from the "Host" control, the values to not update in the "Client" controls as I expected they would.

Can anyone give me a quick explanation of any methods I need to call in order to make sure that the labels refresh with latest data whenever the BindingSource is changed?

Regards,

解决方案

Description

You must implement the INotifyPropertyChanged interface.

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed.

Sample

public class MyClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    private string myProperty;
    public string MyProperty
    {
        get
        {
            return this.myProperty;
        }

        set
        {
            if (value != this.myProperty)
            {
                this.myProperty = value;
                NotifyPropertyChanged("MyProperty");
            }
        }
    }
}

More Information

这篇关于如何在C#用户控制中刷新绑定到DataSource的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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