我有一个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; }
}
}
}
谁能告诉我我做错了什么?
非常感谢!
答案 0 :(得分:1)
如果您使用新的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
答案 1 :(得分:0)
你不应该这样做吗
dataGridView1.DataSource = ScannedChecks.ScannedChecksCollection;