C# Datagridview Binding to a Class not updating

烈酒焚心 提交于 2019-12-11 20:42:06

问题


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!


回答1:


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




回答2:


Shouldn't you be doing

dataGridView1.DataSource = ScannedChecks.ScannedChecksCollection;


来源:https://stackoverflow.com/questions/5529829/c-sharp-datagridview-binding-to-a-class-not-updating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!