C# Datagridview 绑定到类不更新

发布于 2024-10-28 20:10:38 字数 1711 浏览 3 评论 0原文

我有一个 datagridview,我将其绑定到一个类。我添加到类中,但 datagridview 没有更新。

我的绑定:

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

我继续执行 AddCheck 来查看它是否到达了 datagridview,但它没有......不过该类正在更新。

我的班级:

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; }
    }


  }

}

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

非常感谢!

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

My bind:

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

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.

My class:

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?

Thanks much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白色秋天 2024-11-04 20:10:38

如果您用新的 ScannedChecksCollection 替换 ScannedChecksCollection,则属性设置器应触发 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 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));
    }

If checks are modifiable, ScannedCheck should implement INotifyPropertyChanged

‘画卷フ 2024-11-04 20:10:38

你不应该这样做吗

dataGridView1.DataSource = ScannedChecks.ScannedChecksCollection;

Shouldn't you be doing

dataGridView1.DataSource = ScannedChecks.ScannedChecksCollection;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文