8
我有一個小型演示WinForms應用程序。其中一種形式是我的「添加新人員」表單。我使用了詳細信息視圖,而不是使用我的數據源中的DataGridView
。當我輸入數據並單擊導航器上的保存按鈕時,零變化,但我在AddNew
之後以Load
的形式放置MovePrevious
和MoveNext
,一切都按預期工作。爲什麼我需要在更改Binding Source Position之前更改SaveChanges
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()' –
我想你可以調用'EndEdit()'。 doh,@RezaAghaei 5秒打敗我;) – MickyD
EndEdit的作品。謝謝@RezaAghaei,真的對你們倆! – Randy