当前位置:编程学习 > C#/ASP.NET >>

如何在数据变化时触发事件(WinForm)

我有一变量
public static IList<DataInfo> dataList;
我现在将这个数据绑定在一个网格内,
我想在当dataList数据变化时触发一个事件,将数据重新绑定.这个应该怎么弄?

public class DataInfo 

  public int Id{get;set;} 
  public string Value{get;set;} 
}  --------------------编程问答-------------------- 在哪修改就在哪重新绑定吧
--------------------编程问答-------------------- public class Person : INotifyPropertyChanged
{
  private string firstNameValue;
  public string FirstName{
  get { return firstNameValue; }
  set
  {
  firstNameValue=value;
  NotifyPropertyChanged("FirstName");
  }
  }
  public event PropertyChangedEventHandler PropertyChanged;
  public void NotifyPropertyChanged(string propertyName)
  {
  if (PropertyChanged != null)
  {
  PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  }
  }   
}
--------------------编程问答-------------------- 你应该在 dataList数据变化时触发的那个事件中,写上重新绑定的代码就可以了 --------------------编程问答--------------------
引用 1 楼 zenghd 的回复:
在哪修改就在哪重新绑定吧

这个数据变化在很多地方有,要每个变化的地方加个事件不方便维护的. --------------------编程问答--------------------
引用 3 楼 hui717 的回复:
你应该在 dataList数据变化时触发的那个事件中,写上重新绑定的代码就可以了

是呀,我现在就是想数据变化时触发一个事件,重新去绑定一下,
可现在的问题是要怎么样去触发这个事件 --------------------编程问答-------------------- 没人有会么. --------------------编程问答--------------------
引用 2 楼 wuyq11 的回复:
public class Person : INotifyPropertyChanged
{
  private string firstNameValue;
  public string FirstName{
  get { return firstNameValue; }
  set
  {
  firstNameValue=value;
  NotifyPropertyCh……

有现成接口就没必要自己搞多好呀.
--------------------编程问答-------------------- --------------------编程问答-------------------- 有种集合叫做:ObservableCollection


            ObservableCollection<DataInfo> infoList = new ObservableCollection<DataInfo>();
            infoList.CollectionChanged += (s, e) =>
            {
                switch (e.Action)
                {
                    case NotifyCollectionChangedAction.Add:
                        //todo
                        break;
                    case NotifyCollectionChangedAction.Move:
                        //todo
                        break;
                    //other case...
                }
            };
--------------------编程问答-------------------- --------------------编程问答--------------------
引用 2 楼 wuyq11 的回复:
public class Person : INotifyPropertyChanged
{
  private string firstNameValue;
  public string FirstName{
  get { return firstNameValue; }
  set
  {
  firstNameValue=value;
  NotifyPropertyCh……

支持
--------------------编程问答-------------------- BindingSource bs=new BingdingSource(你的list) --------------------编程问答-------------------- 楼主说的数据发生变化是什么意思,是集合里项的数有增减还是项的值发生改变?
如果是数量发生变化,我觉得1楼的方法很合适,在哪里变化就在哪里重新绑定,完全没必要使用事件。
如果是值发生变化,可以借鉴2楼的方法,在属性的Set方法里做事件处理 --------------------编程问答-------------------- public class Person : INotifyPropertyChanged
{
  private string firstNameValue;
  public string FirstName{
  get { return firstNameValue; }
  set
  {
  firstNameValue=value;
  NotifyPropertyChanged("FirstName");
  }
  }
  public event PropertyChangedEventHandler PropertyChanged;
  public void NotifyPropertyChanged(string propertyName)
  {
  if (PropertyChanged != null)
  {
  PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  }
  }   
}
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,