C#的datagridview绑定到一个类没有更​​新 [英] C# Datagridview Binding to a Class not updating

查看:210
本文介绍了C#的datagridview绑定到一个类没有更​​新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView,我绑定到一个类。我加入了类,但在DataGridView没有更新。

I have a datagridview that I am binding to a class. I add to the class but the datagridview is not updating.

我的绑定:

  ScannedChecks = new ScannedChecks();
  ScannedChecks.AddCheck(DateTime.Now, "22222", "checknumdd", "routingdd", _checkData, 4);
  dataGridView1.DataSource = ScannedChecks;

我继续做了AddCheck,看它是否已到达datagridview的,它是不...类被更新,虽然

I went ahead and did the AddCheck to see if it was reaching the datagridview and it isn't... The class is being updated though.

我的类:

namespace SSS.Ckentry
{
  public class ScannedChecks  : INotifyPropertyChanged
  {
    public event PropertyChangedEventHandler PropertyChanged;

    public ScannedChecks()
    {
      ScannedChecksCollection = new ObservableCollection<ScannedCheck>();
    }

    public void AddCheck(DateTime checkDate, string accountNumber, string checkNumber, string bankRoutingNumber, string bankAccountNumber, decimal checkAmount)
    {
      var scc = new ScannedCheck
                  {
                    CheckDate = checkDate,
                    AccountNumber = accountNumber,
                    CheckNumber = checkNumber,
                    BankRoutingNumber = bankRoutingNumber,
                    BankAccountNumber = bankAccountNumber,
                    CheckAmount = checkAmount,
                  };

      ScannedChecksCollection.Add(scc);

    }

    public ObservableCollection<ScannedCheck> ScannedChecksCollection { get; set; }

    public class ScannedCheck
    {
      public DateTime CheckDate { get; set; }
      public string AccountNumber { get; set; }
      public string CheckNumber { get; set; }
      public string BankRoutingNumber { get; set; }
      public string BankAccountNumber { get; set; }
      public decimal CheckAmount { get; set; }
    }


  }

}

谁能告诉我什么,我做错了什么?

Can anyone tell me what I am doing wrong?

谢谢了!

推荐答案

如果你曾经用新ScannedChecksCollection更换ScannedChecksCollection,属性setter应该触发的PropertyChanged exent。

If you ever replace the ScannedChecksCollection with a new ScannedChecksCollection, the property setter should fire the PropertyChanged exent.

    private ObservableCollection<ScannedCheck> scannedChecksCollection;
    public ObservableCollection<ScannedCheck> ScannedChecksCollection {
        get
        {
            return scannedChecksCollection; 
        }
        set
        {
            if (value != scannedChecksCollection)
            {
                value = scannedChecksCollection;
                NotifyPropertyChanged("ScannedChecksCollection");
            }
        }
    }

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

如果检查是可以修改的,应ScannedCheck实现INotifyPropertyChanged

If checks are modifiable, ScannedCheck should implement INotifyPropertyChanged

这篇关于C#的datagridview绑定到一个类没有更​​新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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