2011-04-03 116 views
0

我有一個datagridview,我綁定到一個類。我添加到類,但datagridview不更新。C#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; } 
    } 


    } 

} 

誰能告訴我什麼,我做錯了什麼?

非常感謝!

回答

1

如果你有一個新的ScannedChecksCollection更換ScannedChecksCollection,屬性setter應該火的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

+1

同樣的,他需要在他自己的對象的屬性通知道具的變化而不是快捷獲取/套。 – SQLMason 2011-04-03 13:44:58

+0

And ...而不是AddCheck(...)他可以只是ScannedChecks.Add(新ScannedCheck(){CheckDate = ....}); – SQLMason 2011-04-03 13:47:18

0

你不應該做

dataGridView1.DataSource = ScannedChecks.ScannedChecksCollection;