为什么我需要在SaveChanges之前更改绑定源位置
我有一个小型的WinForms演示应用程序。 其中一个表格是我的添加新人表格。 我使用了Details View而不是Data Sources中的DataGridView
。 当我输入数据并单击导航器上的保存按钮时,没有任何更改,但是我在表单Load
AddNew
之后放置了MovePrevious
和MoveNext
,一切都按预期工作。
public partial class AddPersonForm : Form { private readonly DemoContext _context; public AddPersonForm() { _context = new DemoContext(); InitializeComponent(); } protected override void OnLoad(EventArgs e) { _context.People.Load(); personBindingSource.DataSource = _context.People.Local.ToBindingList(); personBindingSource.AddNew(); personBindingSource.MovePrevious(); personBindingSource.MoveNext(); base.OnLoad(e); } private void personBindingNavigatorSaveItem_Click(object sender, EventArgs e) { int changes = _context.SaveChanges(); Debug.WriteLine("# of changes: " + changes); } }
为什么我需要在识别更改并保存之前切换BindingSource位置?
您不需要更改位置,实际上您需要调用BindingSource.EndEdit
来应用对基础数据源的挂起更改。
更改位置会导致基础货币管理器调用EndCurrentEdit
,这就是绑定源的EndEdit
方法为您所做的事情。